Passing variables from POST to another page using jQuery

I'm relatively new to jQuery, and I was wondering how would one message be sent to another page and then redirected? I used the ajax function, the redirection works fine, but no variables are written to POST (they are empty)

function linkWO() { $.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "LinkTagOut.aspx", dataType: "json", data: "{id=1}", complete: function () { window.location = "LinkTagOut.aspx"; } }); } 

in my aspx file

 <a href="javascript:void(0);" onclick="return linkWO();"><span>Link</span></a> 
+9
jquery redirect post
source share
4 answers

This answer is for quick fix only.

why don't you just pass as the 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; }); 
+2
source share
 $.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 $. ajax documentation ( data ):

Data to send to the server. It is converted to a string request, if not already a string. This is attached to the URL for GET requests. See the processData parameter to prevent this automatic processing. The object must be key / value pairs. If the value is an array, jQuery serializes multiple values ​​using the same key, based on the value of the traditional setting (described below).

Also, make sure that return false from the end of the send handler (or something else calls an ajax call) to make sure that a β€œnormal” redirect does not occur.

+3
source share

See the data option on this page of the document: http://api.jquery.com/jQuery.ajax/

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

However, note that json objects that have passed this path will be converted to a jQuery parameterized query string, so if this is not inconvenient (as in this case, when you have only one value), you can simply pass this in to start and save the script some work.

0
source share

If you like the code less, do the following:

  • include jquery.redirect.min.js in your javascript (js) folder.
  • Download the file AFTER the JQUERY script is loaded:

     <script type='text/javascript' src='jquery.js'> </script> <script type='text/javascript' src='jquery.redirect.min.js'> </script> 
  • Just replace the required parameters on the next line (copy / paste) into javascript code:

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

This is the easiest and fastest method I have found for posting variables on another page without using a form tag. Good luck

0
source share

Source: https://habr.com/ru/post/649812/


All Articles