在Jquery Ajax POST中获得400个错误请求错误

我试图发送一个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"
});

可能是它会帮助别人。

相关文章

1.第一步 设置响应头 header('Access-Control-Allow...
$.inArray()方法介绍 $.inArray()函数用于在数组中搜索指定的...
jquery.serializejson.min.js的妙用 关于这个jquery.seriali...
JS 将form表单数据快速转化为object对象(json对象) jaymou...
jQuery插件之jquery.spinner数字智能增减插件 参考地址:http...