JQuery AJAX Post-pass variable and navigation url

I am developing a little riddle. Users click on image blocks and guess what kind of picture it is. I also count how many blocks they opened before sending. I am using the following jQuery AJAx code in VBScript / ASP

dataString = 'totalClick=' + totalClick ; $.ajax({ type: "POST", url: "puzzle3.asp", data: dataString, success: function() { alert('totalClick' + totalClick + "data: " + dataString ); window.location = "puzzle3.asp"; } }); 

My problem is that I want to get the value of the variable "totalClick" on the page "puzzle3.asp". After I move on to "puzzle3.asp", I want to use it there to solve some other material. But here, as soon as I get to puzzle3.asp through AJAX, I lost the value of the POST variable "totalClick".

Is it possible to use POST, go to the next page and use the vlaue variable there. Just like setting a session variable, but I understand that I cannot assign a session variable using JS.

Any help would be greatly appreciated.

+5
source share
3 answers

from jquery doc:

dataObject, String The data to send to the server. It is converted to a query string if it is not already a string. It is added 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 ​​with the same key based on the value of the traditional setting (described below).

Have you tried manually placing your variable in the query string?

 $.ajax({ type: "POST", url: "puzzle3.asp?totalClick=" + totalClick, data: dataString, success: function() { alert('totalClick' + totalClick + "data: " + dataString ); window.location = "puzzle3.asp"; } }); 
0
source

Try saving the variables via POST, load a new page and extract the variable using AJAX, for example:

Page A:

 $.ajax({ type: "POST", url: "puzzle3.asp", data: dataString, success: function() { alert('totalClick' + totalClick + "data: " + dataString ); window.location = "puzzle3.asp"; } }); 

Page B (puzzle3.asp):

 /** * No need to wait for load() event, just generate a * global variable with the number of clicks */ (function($){ $.ajax({ type: "GET", url: "read_clicks.asp", dataType: "json", success: function (data) { // Use any global variable, as needed $.totalClicks = data.clicks || 0; } }); }(jQuery)); 
0
source

You can use JSON.stringify :

 var ssnData="123"; $.ajax({ type: "POST", contentType: "application/json;charset=utf-8", data: JSON.stringify({ 'SSNdata': ssnData }), url: 'home/data', dataType: 'json', success: function (cicos) { //cicos is your response data } }); 
0
source

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


All Articles