How to send json and parse it on the next html page via url in jquery?

I want to send json data via url to the next html page. I tested it with an emulator, since I work in a mobile application, so the URL cannot be redirected to the next page, and at the moment it is crashing, what is the reason for this. How can I make it out on the next page. Am I new to jquery any idea? my json data contains the result of two different sql queries in an array

$.ajax({ type : "POST", datatype : "json", url : "http://Localhost/phpBB3/check_pass.php?username="+ username + "&password="+ password+"&f=68", success: function(data){ alert(data); window.location.href="source/testmenu.html?varid=" + data +"&username=" + username +"&password=" + password; } }); 

This is the code on the next page.

 $(document).ready(function GetUrlValue(VarSearch){ var SearchString = window.location.search.substring(1); var arr = SearchString.split('&'); console.log(arr); //Set session variables var username = arr[1].split('=')[1]; var password = arr[2].split('=')[1]; document.getElementById('username').value = username; document.getElementById('password').value = password; )}; 
+7
json javascript jquery html
source share
4 answers

in your case on the first page urlencode json

 window.location.href="source/testmenu.html?varid=" + encodeURIComponent(data) +"&username=" + username +"&password=" + password; 

and on the next page

 var data= arr[0].split('=')[1]; var recieved_json = $.parseJSON(data); 
+3
source share

Then try the following:

 var data = { username: username, password: password }; $.ajax({ type: "POST", url: "http://Localhost/phpBB3/check_pass.php", params: $.param(data), success: function(a) { window.location.href = "source/testmenu.html?" + $.param(a) + "&" + $.param(data) } }); 

And this will be your code for the next page (iterator from Satpal answer ):

 $(document).ready(function() { var params = window.location.search; var getURLParams = function(params) { var hash; var json = {}; var hashes = url.slice(url.indexOf('?') + 1).split('&'); for (var i = 0; i < hashes.length; i++) { hash = hashes[i].split('='); json[hash[0]] = hash[1]; } return json; } params = getURLParams(params); var username = params.username; var password = params.password; $('#username').val(username); $('#password').val(password); }); 

Although I agree with @Jai that sending username and password in the url is not recommended.

+2
source share

Once you download the URL, you will need to run your data using encoding and decoding. You may have the wrong path. If you want " http: //Localhost/source/testmenu.html , make sure that the first character is" / ".

Make sure your data object is encoded correctly.

 // Encode data for querystring value. var data = { foo: "bar" }; var data_str = JSON.stringify(data); data_str = encodeURIComponent(data_str); 

Decode and test your URL.

 // Get data from querystring value. // Get the query as an object with decoded values. // Note that JSON values still need parsing. function getQuery() { var s=window.location.search; var reg = /([^?&=]*)=([^&]*)/g; var q = {}; var i = null; while(i=reg.exec(s)) { q[i[1]] = decodeURIComponent(i[2]); } return q; } var q = getQuery(); try { var data = JSON.parse(q.data); } catch (err) { alert(err + "\nJSON=" + q.data); } 
+1
source share

The parameter must be html encoded during navigation or requesting the URL and decoding on the receiving side. It may suspect potentially dangerous content that could lead to a crash.

0
source share

All Articles