
本文介绍了获得400错误的请求错误jQuery中的Ajax POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用JQuery发送一个Ajax POST请求,但我有400错误的请求错误。
下面是我的code:
$。阿贾克斯({
键入:POST,
网址:HTTP://本地主机:8080 /工程/服务器/ REST /科目,
数据: {
主题:题:测试名称,
主题:介绍:创建测试对象,检查POST方法API,
子:标签:Facebook的:作品,脸谱:喜欢],
采样大小:10,
值:科学,机器学习]
},
错误:函数(E){
执行console.log(E);
}
});
它说:不能要求建设资源。我在想什么?
解决方案 最后,我得到了错误的原因是,我需要字符串化的JSON数据,我送
。我必须设置在XHR对象的内容类型和数据类型。因此,正确的版本是在这里:
$。阿贾克斯({
键入:POST,
网址:HTTP://本地主机:8080 /工程/服务器/ REST /科目,
数据:JSON.stringify({
主题:题:测试名称,
主题:介绍:创建测试对象,检查POST方法API,
子:标签:Facebook的:作品,脸谱:喜欢],
采样大小:10,
值:科学,机器学习]
}),
错误:函数(E){
执行console.log(E);
},
数据类型:JSON,
的contentType:应用/ JSON
});
可能会帮助别人。
I am trying to send an Ajax POST request using Jquery but I am having 400 bad request error.
Here is my code:
$.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 ?
解决方案
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"
});
May be it will help someone else.
这篇关于获得400错误的请求错误jQuery中的Ajax POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-24 18:33