[英]How to create and send JSON from client side
I have a web application. 我有一个Web应用程序。 On the client side I basically need to get the values of some fields and turn them into JSON and then send them to the server in an AJAX kind of way.(asynchronous)
在客户端,我基本上需要获取某些字段的值并将它们转换为JSON,然后以AJAX方式将它们发送到服务器。(异步)
How would you do that? 你会怎么做? I also am using jQuery
我也在使用jQuery
If you want to create JSON (also called stringifying) on the client side, you can use the stringifier from json.org. 如果要在客户端创建JSON(也称为字符串化),可以使用json.org中的字符串化器。 More details about its use here .
关于它在这里使用的更多细节。
You would then use your normal jQuery.ajax(...)
like so: 然后,您将使用正常的
jQuery.ajax(...)
如下所示:
function sendJSON(dataToStringify) {
var stringifiedData = JSON.stringify(dataToStringify);
jQuery.ajax({
url: 'http://some.url.here',
data: {stringified: stringifiedData},
success: function(data) {
//code to handle successful AJAX post
},
error(XMLHttpRequest, textStatus, errorThrown) {
//code to handle errors
}
});
}
json2.js allows you to convert JavaScript objects to JSON representations using the JSON.stringify()
function. json2.js允许您使用
JSON.stringify()
函数将JavaScript对象转换为JSON表示。
$.ajax() will allow you to then pass your string as a query parameter to your server side. $ .ajax()将允许您将字符串作为查询参数传递给服务器端。
Quick example to tie them both together: 将它们绑在一起的快速示例:
$.ajax({
url: '/someurl',
data: { json: JSON.stringify(myData) }
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.