Can you send JSON via url?

I have a ruby ​​hash where the keys are the urls and the values ​​are integers. I am converting a hash to JSON, and I wonder if I can send JSON inside the url via an AJAX request, and then pull that JSON from the params hash.

Also, I'm going to send JSONifyed ruby ​​hash back to the client. If I have a successful callback in my AJAX function, where I get the data in the data variable, how do I parse this JSON with jQuery?

Please let me know if I need to be more specific.

+6
source share
2 answers

Yes, you can without a problem. No manual encoding / decoding required!

Your code will look like this:

 var jsonParam = '{"name":"Edgar"}'; //Sample json param $.ajax({ ... type: "get", //This sends in url data: {jsonParam: jsonParam}, //This will encode your json for url automatically dataType: "json", //With this the response will be automatically json-decoded! success: function(response){ //Assuming your server output was '{"lastName":"Villegas"}' as string alert(response.lastName); } }); 

As you can see, manual encoding / decoding is not required. JQuery handles all this!

Hope this helps. Greetings

PS: If for some reason you need to encode / decode json manually to use javascript encodeURIComponent(string) and $.parseJSON(jsonString) methods in JavaScript.

+10
source

Yes, you can pass a json object as a get or post parameter.

To parse a json string using jquery, you can use $ .parseJSON.

+1
source

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


All Articles