Passing variables with POST to another page with Jquery

后端 未结 4 1665
[愿得一人]
[愿得一人] 2021-01-11 21:24

I am relatively new to Jquery and I was wondering how would one post variables to another page and then redirect? I used ajax function, the redirect works fine, but no varia

相关标签:
4条回答
  • 2021-01-11 21:54

    If you like to code less do this:

    1. include jquery.redirect.min.js in your javascript (js) folder.
    2. Load file AFTER JQUERY loading script:

      <script type='text/javascript' src='jquery.js'> 
      </script>
      
      <script type='text/javascript' src='jquery.redirect.min.js'>
      </script>
      
    3. Simply replace desired parameters on the next line (copy/paste) in your javascript code:

      $().redirect('targeturl.html', {'post_var_1': 'value1', 'post_var_2': 'value2'});
      

    This is the simplest and the fastest method I have found for posting variables to another page without using the form tag. Good luck!

    0 讨论(0)
  • 2021-01-11 21:57
    $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "LinkTagOut.aspx",
            dataType: "json",
            data: { id: 1 }, // or the string: 'id=1'
            complete:
            function () {
                window.location = "LinkTagOut.aspx";
            }
    
    });
    

    From the $.ajax documentation (data option):

    Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

    Also, make sure to return false from the end of the submit handler (or whatever fires the ajax call) to ensure that a 'normal' redirect is not happening.

    0 讨论(0)
  • 2021-01-11 22:03

    Have a look at the "data" option on this doc page: http://api.jquery.com/jQuery.ajax/

    Your problem is that you are trying to pass a json string (and it is valid to pass a string), but if you pass a string, jQuery is expecting a parameterized query string. If you want to pass a json object, it should not be a string.

    However, note that json objects passed this way will be converted to a parameterized query string by jQuery, so if it's not inconvenient (as in this case, where you only have one value), you might as well just pass it that way to begin with and save the script some work.

    0 讨论(0)
  • 2021-01-11 22:15

    This answer is just for a quick fix

    why don't you just pass as query string here

    window.location = "LinkTagOut.aspx?variabletopass=test"; 
    
    <form id="target" action="destination.html">
      <input type="text" value="Hello there" />
      <input type="submit" value="Go" />
    </form>
    <div id="other">
      Trigger the handler
    </div>
    
    $('#target').submit(function() {
    $.post('ajax/test.html', function(data) {      
    });
      return false;
    });
    
    0 讨论(0)
提交回复
热议问题