jquery AJAX 和 json 格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17426199/
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
jquery AJAX and json format
提问by Jorg Ancrath
I have a webservice that expects to receive json, like so:
我有一个希望接收 json 的网络服务,如下所示:
{"first_name":"test","last_name":"teste","email":"[email protected]","mobile":"+44 22 2222 2222", "password":"testing"}
My ajax call in jquery:
我在 jquery 中的 ajax 调用:
$.ajax({
type: "POST",
url: hb_base_url + "consumer",
contentType: "application/json",
dataType: "json",
data: {
first_name: $("#namec").val(),
last_name: $("#surnamec").val(),
email: $("#emailc").val(),
mobile: $("#numberc").val(),
password: $("#passwordc").val()
},
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
Is there any way to check the format in which my data is being sent? I'm supposedly not sending correct JSON to the server (that is the first step in validation).
有什么方法可以检查我的数据发送的格式吗?我应该没有向服务器发送正确的 JSON(这是验证的第一步)。
Is my jquery code sending valid JSON or did I miss something?
我的 jquery 代码发送了有效的 JSON 还是我错过了什么?
回答by MrCode
You aren't actually sending JSON. You are passing an object as the data
, but you need to stringify the object and pass the string instead.
您实际上并不是在发送 JSON。您将对象作为 传递data
,但您需要将对象字符串化并传递字符串。
Your dataType: "json"
only tells jQuery that you want it to parse the returned JSON, it does not mean that jQuery will automatically stringify your request data.
您dataType: "json"
只是告诉 jQuery 您希望它解析返回的 JSON,这并不意味着 jQuery 会自动对您的请求数据进行字符串化。
Change to:
改成:
$.ajax({
type: "POST",
url: hb_base_url + "consumer",
contentType: "application/json",
dataType: "json",
data: JSON.stringify({
first_name: $("#namec").val(),
last_name: $("#surnamec").val(),
email: $("#emailc").val(),
mobile: $("#numberc").val(),
password: $("#passwordc").val()
}),
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
回答by user1477388
I never had any luck with that approach. I always do this (hope this helps):
我对这种方法从来没有运气。我总是这样做(希望这会有所帮助):
var obj = {};
obj.first_name = $("#namec").val();
obj.last_name = $("#surnamec").val();
obj.email = $("#emailc").val();
obj.mobile = $("#numberc").val();
obj.password = $("#passwordc").val();
Then in your ajax:
然后在你的 ajax 中:
$.ajax({
type: "POST",
url: hb_base_url + "consumer",
contentType: "application/json",
dataType: "json",
data: JSON.stringify(obj),
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
回答by Emil A.
Currently you are sending the data as typical POST values, which look like this:
当前,您将数据作为典型的 POST 值发送,如下所示:
first_name=somename&last_name=somesurname
If you want to send data as json you need to create an object with data and stringify it.
如果您想将数据作为 json 发送,您需要使用数据创建一个对象并将其字符串化。
data: JSON.stringify(someobject)
回答by ermoshhkin
$.ajax({
type: "POST",
url: hb_base_url + "consumer",
contentType: "application/json",
dataType: "json",
data: {
data__value = JSON.stringify(
{
first_name: $("#namec").val(),
last_name: $("#surnamec").val(),
email: $("#emailc").val(),
mobile: $("#numberc").val(),
password: $("#passwordc").val()
})
},
success: function(response) {
console.log(response);
},
error: function(response) {
console.log(response);
}
});
(RU)На сервере ваши данные можно получить как - $_POST['data__value']; Например для получения значения first_name на сервере, нужно написать:
(RU)На сервере ваши данные можно получить как - $_POST['data__value']; Например для получения значения first_name на сервере, нужно написать:
(EN)On the server, you can get your data as - $_POST ['data__value']; For example, to get the first_name value on the server, write:
(CN)在服务器上,您可以将您的数据作为 - $_POST ['data__value']; 例如,要获取服务器上的 first_name 值,请编写:
$test = json_decode( $_POST['data__value'] );
echo $test->first_name;