加载资源失败:服务器在 Java RESTful Web 服务调用中响应状态为 415(不支持的媒体类型)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39271229/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Failed to load resource: the server responded with a status of 415 (Unsupported Media Type) in Java RESTful web service call
提问by toing_toing
I have the following javascript in my test html page to send ajax requests to a java restful web service I built with netbeans (mostly auto generated by using 'Restful web services from database' function). Here is the ajax query from my test html page:
我的测试 html 页面中有以下 javascript,用于将 ajax 请求发送到我用 netbeans 构建的 java restful web 服务(主要是使用“数据库中的 Restful web 服务”功能自动生成的)。这是来自我的测试 html 页面的 ajax 查询:
$(function(){
$('.message-button').on('click', function(e){
var resultDiv = $("#resultDivContainer");
$.ajax({
headers: { 'Accept': 'application/json',
'Content-Type': 'application/json'
},
'type': 'POST',
'url': 'http://localhost:8080/xxxAPI/api/activity',
'data': { "baseItemId": "2" },
'dataType':'json',
'success': function(data) {
var xmlstr = data.xml ? data.xml : (new XMLSerializer()).serializeToString(data);
$("#resultDivContainer").text(xmlstr);
},
'error': function(jqXHR, textStatus, errorThrown) {
alert(' Error in processing! '+textStatus + 'error: ' + errorThrown);
}
});
})
});
Also here is the part of my java code that accepts post requests:
这里也是我的 java 代码中接受 post 请求的部分:
@POST
@Override
@Consumes({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public void create(XxxxxActivity entity) {
super.create(entity);
}
When I request from the test page (for this version of the test page), I get this error:
当我从测试页面(对于此版本的测试页面)请求时,出现此错误:
Failed to load resource: the server responded with a status of 415 (Unsupported Media Type)
加载资源失败:服务器响应状态为 415(不支持的媒体类型)
or this error:
或者这个错误:
POST http://localhost:8080/xxxAPI/api/activity415 (Unsupported Media Type)
POST http://localhost:8080/xxxAPI/api/activity415(不支持的媒体类型)
So far I have tried making various changes to the ajax request as advised on similar questions on stackoverflow, including changing type to jsonp
, putting json data in double quotes, adding headers and changing data type to xml. None of them have worked.
到目前为止,我已尝试按照有关 stackoverflow 的类似问题的建议对 ajax 请求进行各种更改,包括将类型更改为jsonp
、将 json 数据放在双引号中、添加标题并将数据类型更改为 xml。他们都没有工作。
Also since I manage to get a response from the server at times, I wonder if the issue is with xml parsing in the java code. I believe a potential fix would be to add the Hymanson jar files, but I have no idea how to add them on netbeans as there is no lib folder in WEB_INF.
此外,由于我有时设法从服务器获得响应,我想知道问题是否与 java 代码中的 xml 解析有关。我相信一个潜在的解决方法是添加 Hymanson jar 文件,但我不知道如何将它们添加到 netbeans 上,因为 WEB_INF 中没有 lib 文件夹。
I would also like to know if there is any issue with the jquery ajax request. This issue has been bugging me for days.
我还想知道 jquery ajax 请求是否有任何问题。这个问题已经困扰我好几天了。
PS: Also note that GET requests from the browser work fine. I have not used maven in this project.
PS:还要注意来自浏览器的 GET 请求工作正常。我在这个项目中没有使用过 maven。
回答by grajsek
Replace
代替
'data': { "baseItemId": "2" },
with
和
'data': JSON.stringify({ "baseItemId": "2" }),
Object JSON is available here.
对象 JSON 可在此处获得。
EDIT
编辑
- add attribute
contentType: 'application/json; charset=UTF-8'
- remove attribute headers from ajax call.
- 添加属性
contentType: 'application/json; charset=UTF-8'
- 从 ajax 调用中删除属性头。