在 Jquery Ajax POST 中出现 400 个错误的请求错误 [英] Getting 400 bad request error in Jquery Ajax POST
本文介绍了在 Jquery Ajax POST 中出现 400 个错误的请求错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用 Jquery 发送 Ajax POST 请求,但出现 400 个错误请求错误.
I am trying to send an Ajax POST request using Jquery but I am having 400 bad request error.
这是我的代码:
$.ajax({
type: 'POST',
url: "http://localhost:8080/project/server/rest/subjects",
data: {
"subject:title":"Test Name",
"subject:description":"Creating test subject to check POST method API",
"sub:tags": ["facebook:work", "facebook:likes"],
"sampleSize" : 10,
"values": ["science", "machine-learning"]
},
error: function(e) {
console.log(e);
}
});
它说:无法从请求构建资源.我错过了什么?
It Says: Can not build resource from request. What am I missing ?
推荐答案
最后,我犯了错误,原因是我需要对我发送的 JSON 数据进行字符串化.我必须在 XHR 对象中设置内容类型和数据类型.所以正确的版本在这里:
Finally, I got the mistake and the reason was I need to stringify the JSON data I was sending. I have to set the content type and datatype in XHR object. So the correct version is here:
$.ajax({
type: 'POST',
url: "http://localhost:8080/project/server/rest/subjects",
data: JSON.stringify({
"subject:title":"Test Name",
"subject:description":"Creating test subject to check POST method API",
"sub:tags": ["facebook:work", "facebook:likes"],
"sampleSize" : 10,
"values": ["science", "machine-learning"]
}),
error: function(e) {
console.log(e);
},
dataType: "json",
contentType: "application/json"
});
也许它会帮助别人.
这篇关于在 Jquery Ajax POST 中出现 400 个错误的请求错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文