jquery ajax rest call

编程入门 行业动态 更新时间:2024-10-28 02:27:24
本文介绍了jquery ajax rest call - 不支持的媒体类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在对休息服务进行简单的jquery ajax调用。我将contentType设置为application / json,其余资源配置为接受 MediaType.APPLICATION_JSON 。这是一个POST方法。 使用此设置,我收到不支持的媒体类型错误。

I'm having a simple jquery ajax call to a rest service. I am setting the contentType as "application/json" and the rest resource is configured to accept "MediaType.APPLICATION_JSON". This is a POST method. With this setup, I am getting "Unsupported Media Type" error.

标题信息显示 请求标题中的Content-Type application / json; charset = UTF-8

The header info shows "Content-Type application/json; charset=UTF-8" in the request header

响应显示:状态报告:不支持的媒体类型服务器拒绝了此请求,因为请求实体的格式不受支持所请求方法的请求资源(不支持的媒体类型)。

Response shows: Status report: Unsupported Media Type The server refused this request because the request entity is in a format not supported by the requested resource for the requested method (Unsupported Media Type).

请提供一些指示来解决此问题。

Please provide some pointers to resolve this issue.

以下是代码段:

休息资源

@POST @Produces({MediaType.APPLICATION_JSON,MediaType.TEXT_HTML}) @Consumes({MediaType.APPLICATION_JSON,MediaType.TEXT_HTML}) public Response addPerson(MyJSONObj myObj) { //... // ... //... }

jquery

$(document).ready(function() { /* put your stuff here */ $("#Button_save").click(function(){ var firstName = $('firstName').val(); var lastName = $('lastName').val(); var person = {firstName: firstName, lastName: lastName}; $.ajax({ url:'localhost:8080/sampleApplication/resources/personRestService/', type: 'POST', data: person, Accept : "application/json", contentType: "application/json", success:function(res){ alert("it works!"); }, error:function(res){ alert("Bad thing happend! " + res.statusText); } }); }); });

FF Firebug中显示的标题

回复标题

Content-Length 1117 Content-Type text/html;charset=utf-8 Date Thu, 05 Apr 2012 09:44:45 GMT Server Apache-Coyote/1.1

请求标题

Accept */* Accept-Encoding gzip, deflate Accept-Language en-us,en;q=0.5 Connection keep-alive Content-Length 97 Content-Type application/json; charset=UTF-8 Host localhost:8080 Referer localhost:8080/sampleApplication/ User-Agent Mozilla/5.0 (Windows NT 5.1; rv:11.0) Gecko/20100101 Firefox/11.0 X-Requested-With XMLHttpRequest

推荐答案

<我有同样的问题,我能够以这种方式解决它(参见 http:// www.weverwijk/wordpress/tag/jquery/ ):

$.ajax({ url:'localhost:8080/sampleApplication/resources/personRestService/', type:'POST', data: JSON.stringify(person), dataType: 'json', contentType: "application/json; charset=utf-8", success:function(res){ alert("it works!"); }, error:function(res){ alert("Bad thing happend! " + res.statusText); } });

在Java方面,我添加了这些(参见 Access-Control-Allow-Origin ):

On Java side I added these (see Access-Control-Allow-Origin ):

@OPTIONS public Response testt(@Context HttpServletResponse serverResponse) { serverResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); serverResponse.addHeader("Access-Control-Allow-Credentials", "true"); serverResponse.addHeader("Access-Control-Allow-Origin", "*"); serverResponse.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With"); serverResponse.addHeader("Access-Control-Max-Age", "60"); return Response.ok().build(); } @POST @Produces(MediaType.APPLICATION_JSON) @Consumes(APPLICATION_JSON) public Response addPerson(MyJSONObj myObj, @Context HttpServletResponse serverResponse) serverResponse.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD"); serverResponse.addHeader("Access-Control-Allow-Credentials", "true"); serverResponse.addHeader("Access-Control-Allow-Origin", "*"); serverResponse.addHeader("Access-Control-Allow-Headers", "Content-Type,X-Requested-With"); serverResponse.addHeader("Access-Control-Max-Age", "60"); // ... // ... }

结论

  • JSON对象自动转换和转换(更多细节见为RESTful Web服务配置JSON )
  • POST commit
  • 跨域(Same-Origin-Policy)
  • Firefox可以工作(参见@Option标签)
  • JSON Object gets transfered and transformed automagically (more details see Configuring JSON for RESTful Web Services)
  • POST commit
  • Cross Domain (Same-Origin-Policy)
  • Firefox works (see @Option tag)

更多推荐

jquery ajax rest call

本文发布于:2023-11-16 21:21:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1607508.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ajax   jquery   call   rest

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!