Javascript 使用JQuery将JSON上载到基于Dropwizard的API时出现HTTP 415错误
我已经使用Dropwizard编写了一个REST/JSON API。除了内部单元测试之外,我还使用restletcrome扩展和curl对其进行了测试。 我一直在HTTP请求中使用内容类型:“application/json”头。 这正是我们想要的 现在我正在开发一个基于JavaScript的前端,它可以访问API。我没有深入研究JavaScript,而是使用JQuery。它可以很好地处理GET请求。最后一天我一直在努力让POST请求正常工作。我不知道我做错了什么。非常感谢您的任何帮助 关于API:Dropwizard资源类如下所示:Javascript 使用JQuery将JSON上载到基于Dropwizard的API时出现HTTP 415错误,javascript,java,jquery,json,dropwizard,Javascript,Java,Jquery,Json,Dropwizard,我已经使用Dropwizard编写了一个REST/JSON API。除了内部单元测试之外,我还使用restletcrome扩展和curl对其进行了测试。 我一直在HTTP请求中使用内容类型:“application/json”头。 这正是我们想要的 现在我正在开发一个基于JavaScript的前端,它可以访问API。我没有深入研究JavaScript,而是使用JQuery。它可以很好地处理GET请求。最后一天我一直在努力让POST请求正常工作。我不知道我做错了什么。非常感谢您的任何帮助 关于AP
@Path("/documents")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class DocumentsResource {
//generic stuff happening in the class, including internal data handling and the add() method
@POST
public Response addDocument(@NotNull @Valid TCDocuments documents){
if(documents.getDocuments().length==0){
Response response = Response.status(400).build();
return response;
}
else if(documents.getDocuments().length>0){
for(int i=0; i<documents.getDocuments().length; i++){
TCDocument doc=documents.getDocuments()[i];
add(doc);
}
}
Response response = Response.ok().build();
return response;
}
}
我尝试过使用JQuery发布多种内容。在我的HTML页面中,有一个名为“createF”的表单,我在发布到该界面时使用该表单输入数据。如前所述,我并不真正适合JavaScript,但根据StackOverflow上的建议,我在JavaScript中创建了一个documents对象,然后尝试发送它:
var form = $("#createF").serializeArray();
var documents = {
documents: [
{
id: form[0].value,
label: form[1].value,
content: form[2].value,
url: form[3].value
}
]
};
var json = JSON.stringify(documents);
使用我发现的console.log()命令,stringify将id放在引号中。我的API不接受这一点,所以我认为这是发生415个错误的原因。为了解决这个问题,我使用字符串运算符将一些内容组合在一起:
var json = "{ \"documents\":[{\"id\":-1,\"label\":\"metadata json\",\"content\":\"I am ignored\",\"url\":\"\"}]}";
不幸的是,当我尝试使用以下方式发送时,这也会导致415个错误:
美元邮政(
“/文件”,
json,
功能(响应){
控制台日志(resonse);
},
“json”
);
正如我在这里发现的,我不仅应该将内容类型设置为HTTP头,还应该将其设置为Accepted=“application/json”。我不需要在restlet客户机或curl中指定该头。但也许JQuery的行为有所不同。我知道什么?因此,我编写了以下代码:
var url="../documents";
$.postJSON = function(url, json){
return jQuery.ajax({
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
'type': 'POST',
'url': url,
'data': json,
'dataType': 'json',
'success': function(){
console.log("successfully posted something");
}
});
};
好消息是,这并没有产生415条消息,但我根本没有收到API的回复。网络日志显示,在调用函数时,没有包离开浏览器。
我在这里和其他方面所发现的基本上都是关于如何在JQuery中使用$.post或$postJSON的更多信息。我尝试了所有的示例变体,得到了更多415条消息
谁能告诉我我做错了什么或者我应该做什么?提前非常感谢 谢谢@Justin Pearce的评论。使用$.ajax()而不是$.post()或$.postJSON()时,以下代码有效:
$("#createF").submit(function( event ) {
event.preventDefault();
console.log("Uploading data");
var form = $("#createF").serializeArray();
console.log(form);
var json = "{ \"documents\":[{\"id\":"+form[0].value+
",\"label\":\""+form[1].value+
"\",\"content\":\""+form[2].value+
"\",\"url\":\""+form[3].value+"\"}]}";
console.log(json);
var url="../documents";
$.ajax({
url: url,
headers: {
'Accept': 'application/json',
'Content-Type':'application/json'
},
method: 'POST',
dataType: 'json',
data: json,
success: function(data){
console.log('something worked');
console.log('succes: '+data);
}
});
});
唯一有趣的是,即使操作成功运行,success函数似乎也不会被触发。我将API的响应代码从200更改为201(已创建)。这似乎也不会触发成功功能。否则,这就是它应该做的。您的
Accept':“application/json”
似乎在Accept
Hello@Justin Pearce前面缺少一个引号,谢谢您的评论。在将其发布到stackoverflow并修复缩进时,这是一个复制粘贴错误。在原始来源中有引号。我更新了我原来的问题。
$("#createF").submit(function( event ) {
event.preventDefault();
console.log("Uploading data");
var form = $("#createF").serializeArray();
console.log(form);
var json = "{ \"documents\":[{\"id\":"+form[0].value+
",\"label\":\""+form[1].value+
"\",\"content\":\""+form[2].value+
"\",\"url\":\""+form[3].value+"\"}]}";
console.log(json);
var url="../documents";
$.ajax({
url: url,
headers: {
'Accept': 'application/json',
'Content-Type':'application/json'
},
method: 'POST',
dataType: 'json',
data: json,
success: function(data){
console.log('something worked');
console.log('succes: '+data);
}
});
});