jQuery 通过 AJAX 发送 JSON POST 请求
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22094890/
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
Sending a JSON POST request via AJAX
提问by Hagbard
I'm using POST to submit data to the server, this error appears in the console:
我正在使用 POST 向服务器提交数据,控制台中出现此错误:
POST localhost/myProject/webapp/setAccount 400 (Required String parameter 'accountID' is not present)
POST localhost/myProject/webapp/setAccount 400(不存在必需的字符串参数“accountID”)
The user will select from a combo box an account ID and this is what should be passed in the post.
用户将从组合框中选择一个帐户 ID,这是应该在帖子中传递的内容。
Here is my code:
这是我的代码:
accountSelected: function () {
var accountSelected = $("#accountcombobox").val();
console.log("Selected Account: " + accountSelected);
var myUrl = "webapp/setAccount";
$.ajax({
url: myUrl,
type: "POST",
data: accountSelected,
dataType: "json",
contentType: "application/json"
})
.done(function (data) {
console.log("Response " + JSON.stringify(data));
})
}
The first console.log shows the selected account ID as expected so i know the value is correct. I want to pass this value to the POST request, which is what i thought i was doing but cannot seem to get it to work.
第一个 console.log 按预期显示选定的帐户 ID,因此我知道该值是正确的。我想将此值传递给 POST 请求,这就是我认为我正在做的事情,但似乎无法让它工作。
EDIT
编辑
Thanks for the responses but none of the answers seem to be working for me, still the same error.
感谢您的回复,但似乎没有一个答案对我有用,仍然是同样的错误。
I'm also using Marionette and Backbone, could this effect it?
我也在用木偶和骨干,这会影响吗?
回答by Krish R
Try this,
尝试这个,
data:{ accountSelected: accountSelected},
AND,
和,
console.log("Response " + data); //removed JSON.stringify
in webapp/setAccount,
在 webapp/setAccount 中,
echo $_POST['accountSelected'];
回答by Alex
Your data should be in key: value form, so:
你的数据应该是 key: value 形式,所以:
Edit: changed to success
编辑:更改为成功
$.ajax({
url: myUrl,
type: "POST",
data:{
accountID: accountSelected
},
dataType: "json",
contentType: "application/json",
success: function(data)
{
console.log("Response " +data.accountSelected);
}
});
回答by Selva
accountSelected: function () {
var accountSelected = $("#accountcombobox").val();
console.log("Selected Account: " + accountSelected);
var myUrl = "webapp/setAccount";
$.ajax({
url: myUrl,
type: "POST",
data: "accountID="+accountSelected,
dataType: "json",
contentType: "application/json",
success: function(result) {
console.log("Response " + JSON.stringify(result));
},
error : function(request, textStatus, errorThrown) {
alert('textStatus ' + textStatus);
alert('errorThrown ' + errorThrown);
}
});
}
回答by kamesh
No need to use JSON.stringify coz you already mentioned dataType:'json' so try this
不需要使用 JSON.stringify 因为你已经提到过 dataType:'json' 所以试试这个
$.ajax({
url: myUrl,
type: "POST",
data: { accountSelected: accountSelected},
dataType: "json",
contentType: "application/json"
})
.done(function (data) {
console.log("Response " +data.accountSelected);
});