我试图发送一个Ajax POST请求使用Jquery但我有400错误的请求错误。
这里是我的代码:
$.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); } });
它说:不能从请求构建资源。
我缺少什么?
解决方法
最后,我得到了错误,原因是我需要stringify我发送的JSON数据。我必须在XHR对象中设置内容类型和数据类型。
所以正确的版本是这里:
所以正确的版本是这里:
$.ajax({ type: 'POST',data: JSON.stringify({ "subject:title":"Test Name","machine-learning"] }),error: function(e) { console.log(e); },dataType: "json",contentType: "application/json" });
可能是它会帮助别人。