HTTP状态415-使用JERSEY实现的JQUERY到Restful WS的AJAX调用不支持的媒体类型 [英] HTTP Status 415 - Unsupported Media Type for AJAX call in JQUERY to Restful WS implemented with JERSEY
本文介绍了HTTP状态415-使用JERSEY实现的JQUERY到Restful WS的AJAX调用不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试将json数据发布到使用Jersey实现的Restful WS.我正在通过jquery-ajax发布数据.为什么我得到HTTP Status-415不支持的媒体类型?谢谢.
点击此处查看萤火虫说明的屏幕截图
Hi I am trying to post json data to Restful WS implemented with Jersey. I am posting data through jquery-ajax. Why am I geting HTTP Status-415 unsupported Media type? Thank you.
Click here for screenshot of firebug description
//post method handler
@Path("/newentry")
public class NewEntry {
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response newEntry(String data) {
//doStuff
}
}
// ajax call
$.ajax({
url: "http://localhost:8080/FirstRestWebService/rest/newentry",
type: "post",
data: formToJSON(),
dataType : "json",
success: function(data){
alert("success");
},
error:function(jqXHR, textStatus, errorThrown) {
alert("failure");
}
});
function formToJSON() {
return JSON.stringify({
"name": $("input#emp_name").val(),
...
"username": $('input#username').val(),
"password": $('input#password').val()
});
点击此处查看萤火虫说明的屏幕截图 我能够通过Jersey客户端成功测试WS .上面的AJAX调用有什么问题?谢谢.
Click here for screenshot of firebug description I was able to test the WS successfully by Jersey Client. What is wrong in the above AJAX call? Thank you.
推荐答案
在AJAX调用中,您需要设置内容类型:
In your AJAX call you need to set your content type:
contentType: "application/json"
这篇关于HTTP状态415-使用JERSEY实现的JQUERY到Restful WS的AJAX调用不支持的媒体类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文