Best way to pass JSON from Browser to PHP using Ajax.Request

帅比萌擦擦* 提交于 2019-12-10 15:19:34

问题


Hi I have a JSON object that is a 2-dimentional array and I need to pass it to PHP using Ajax.Request (only way I know how). ...Right now I manually serialized my array using a js function...and get the data in this format: s[]=1&d[]=3&[]=4 etc. ....

my question is: Is there a way to pass the JSON object more directly/efficientely?..instead of serializing it myself?

Thanks for any suggestions, Andrew


回答1:


You can also use Prototype's function toJSON() to convert an array into a JSON object. After passing it to server via Ajax call, simply use PHP's function json_decode() to decode the object.




回答2:


Pass the object as a JSON-string to PHP, and in PHP use the builtin json_decode to get a PHP-object from the string.

In Javascript, use a "stringify" function on your object to get it as a string, library available for example here: https://github.com/douglascrockford/JSON-js/blob/master/json2.js




回答3:


In que Javascript side (with Prototye):

var myJSON= Object.toJSON(youArray);

In que Php side:

$myjson = $_POST['myjson'];

$arrayJSON= json_decode(stripslashes($myjson), true);



回答4:


Check http://www.openjs.com/scripts/data/ued_url_encoded_data/ to encode nested data directly correct, since Object.toQueryString() doesn't accept nested data...



来源:https://stackoverflow.com/questions/311987/best-way-to-pass-json-from-browser-to-php-using-ajax-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!