Javascript 如何将变量转换为json?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5793540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert variables into json?
提问by user622378
I want to send json data to ajax but how do you convert variables into json or convert an array to json?
我想将 json 数据发送到 ajax,但是如何将变量转换为 json 或将数组转换为 json?
$(".confirm_order").click(function(event) {
event.preventDefault();
var street = $("#street").val();
var location = $("#location").val();
var number = $("#number").val();
var f = ???
$.ajax({
type: 'post',
url: "/orders",
dataType: "json",
data: f,
success: function (l) {
alert("Done");
}
});
});
回答by Felix Kling
If you really want to convert the data into JSON, you have to create an object or array and use JSON.stringify
(available in newer browser and can be loaded form here):
如果您真的想将数据转换为 JSON,您必须创建一个对象或数组并使用JSON.stringify
(在较新的浏览器中可用,可以在此处加载表单):
var f = JSON.stringify({street: street, location: location, number: number});
but you cannot just set the data
attribute to f
then. You have to assign it to another variable:
但您不能只将data
属性设置为f
then。您必须将其分配给另一个变量:
data: {data: f}
This will produce the POST parameters like so:
这将产生像这样的 POST 参数:
data={"number":"value of number","location:...}
However, there is not reason to create JSON here. I would sent the values as normal post parameters. For that you just create an object like above and assign it to data
:
但是,没有理由在此处创建 JSON。我会将这些值作为正常的后期参数发送。为此,您只需创建一个像上面一样的对象并将其分配给data
:
data: {street: street, location: location, number: number}
This will create the POST parameters:
这将创建 POST 参数:
street=valueofstreet&location=valueoflocation&...
This would be easier as you don't have to parse the JSON at the server side.
这会更容易,因为您不必在服务器端解析 JSON。
回答by Darin Dimitrov
If you want to send a JSON formatted request to the server you could specify the proper content type for this request and then use the JSON.stringify
method:
如果您想向服务器发送 JSON 格式的请求,您可以为此请求指定正确的内容类型,然后使用以下JSON.stringify
方法:
var street = $('#street').val();
var location = $('#location').val();
var number = $('#number').val();
$.ajax({
type: 'post',
url: '/orders',
dataType: 'json',
data: JSON.stringify({ street: street, location: location, number: number }),
contentType: 'application/json; charset=utf-8',
success: function (l) {
alert("Done");
}
});
This will send the following in the POST body:
这将在 POST 正文中发送以下内容:
{ street: 'foo', location: 'bar', number: 'baz' }
Obviously the server side script you are sending this AJAX to need to be capable of handling and parsing JSON strings.
显然,您发送此 AJAX 的服务器端脚本需要能够处理和解析 JSON 字符串。
回答by Naren Sisodiya
var f = {}
f['street'] = $("#street").val();
f['location'] = $("#location").val();
f['number'] = $("#number").val();
回答by Dave
var f = {
"street": street,
"location": location,
"number": number
};
回答by Quentin
回答by Aivan Monceller
try this. values will be replaced respectively
尝试这个。值将分别被替换
{street : street,location : location, number : number}
{street : street,location : location, number : number}