How can I send POST data and navigate with JQuery?

前端 未结 4 1909
[愿得一人]
[愿得一人] 2021-01-02 19:51

On my blog I have a lot of

 blocks containing code snippets.

What I want to do is add a .click() handler to all the

4条回答
  •  执笔经年
    2021-01-02 20:24

    Yes, you have to create a form and submit it. You can do all sorts of things with ajax posts/gets but the only way to navigate to a post result is via an actual form post. Here is concise version of it:

    $('
    ').attr('action', action).html(html).appendTo('body').submit();

    My code does this:

    // Navigate to Post Response, Convert first form values to query string params:
    // Put the things that are too long (could exceed query string limit) into post values
    var form = $('#myForm');
    var actionWithoutQueryString = form[0].action.split("?")[0];
    var action = actionWithoutQueryString + '?' + $.param(form.serializeArray());
    var html = myArray.map(function(v, i) { return ""; }).join("\n");
    $('').attr('action', action).html(html).appendTo('body').submit();
    

提交回复
热议问题