[英]How to use a JSON-like JQuery function?
How can I use JSON-like variable in my function like this? 我如何在这样的函数中使用类似JSON的变量?
$.ajax({
type:"POST",
-->data:{this_var:this_value}
});
Use JSON.stringify() to create JSON object. 使用JSON.stringify()创建JSON对象。
$.ajax({
type: "POST",
url: urlAction,
dataType: "json",
contentType: "application/json",
data: JSON.stringify({variable1: value1, variable2: value2})
});
Your code: 您的代码:
$.ajax({
type:"POST",
-->data:{this_var:this_value}
});
Then to use the object as you mentioned would be: 然后使用您提到的对象将是:
var myObj = {this_var:this_value};
myObj.this_var();
That is if this_var is a function. 就是说this_var是一个函数。 Otherwise the value can be consumed such as:
否则,可以使用该值,例如:
var myObj = {this_var:this_value};
var myVal = myObj.this_var;
You may also need to pass the data in as a string and then parse it as JSON after getting the string from the ajax call. 您可能还需要将数据作为字符串传递,然后从ajax调用获取字符串后将其解析为JSON。
var myObj = JSON.parse(data);
var myVal = myObj.this_var(); // if this is a function
Try this 尝试这个
formData = {
param1: $("#param1").val(),
param2: $("#param2").val()
}
$.ajax({
type: "POST",
url: "http://example.com/create.php",
data: formData,
dataType: "json",
success: function(data) {
alert("Add success");
},
error: function() {
alert("Add failure");
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.