The interfaces in the project are written in Springmvc, where the consumes= "Application/json" is defined in the @requestmapping interface, which means that the interface only accepts ContentType as application/ The JSON request.
Interface write good tool test interface can be normal request, but in the browser with Ajax request is not.
Baidu, originally in the use of Ajax cross-domain request, if SET header contenttype is Application/json, will be sent two times the request. The first time you send the option request to the server, this request will ask the server which request methods (Get,post, etc.) are supported, which request headers are supported, and so on. When this request returns, the second request will continue to be sent if the request that we are going to send is in accordance with the server's rules, otherwise the error will be in the console.
Why do I request two times when I set ContentType to Application/json in cross-domain? In fact, jquery's documentation explains this.
ContentType (default: ' application/x-www-form-urlencoded; Charset=utf-8 ')
Type:boolean or String
When you sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; Charset=utf-8 ", which is fine for the most cases. If you explicitly pass in a content-type to $.ajax () and then it's always sent to the server (even if no data is sent). As of jquery 1.6 you can pass false to tell JQuery to not set any content type header. note:the XMLHttpRequest specification dictates that the charset are always UTF-8; specifying another charset would The browser to the encoding. Note:for Cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, Multip Art/form-data, or Text/plain would trigger the browser to send a preflight OPTIONS request to the server.
Note the following description, in the cross-domain, in addition to the ContentType is Application/x-www-form-urlencoded,multipart/form-data or text/plain outside, Will trigger the browser to send the option request first.
For example, your original request is a method method post, if the first request returns the result header of the Allow attribute and there is no post method, then the second request is not sent, the browser console will be error, tell you that the POST method is not supported by the server.
The arrows in the figure point to allow is the supported method returned by the server.
not only that, if you want to send cross-domain requests with Contenttype:application/json, the server side must also set a header named Access-control-allow-headers, setting its value to Content-type, which indicates that the server is able to receive the ContentType attribute in the request sent by the front end and uses its value . Otherwise the second request can not be sent out, the browser console will error, and you are prompted that the server is not set access-control-allow-headers.
That corresponds to the filter filter in my Java program.
1 Public classMyfilterImplementsFilter {2 3 @Override4 Public voiddestroy () {5 //System.out.println ("Filter Destruction");6 }7 8 @Override9 Public voidDoFilter (servletrequest request, servletresponse response,TenHttpServletRequest HttpRequest =(httpservletrequest) request; OneString type=Httprequest.getmethod (); A - if(Type.touppercase (). Equals ("OPTIONS") = =true) { - Httpresponse.setheader ("Access-control-allow-headers", "Content-type, accept"); Httpresponse.setheader ( "Access-control-allow-methods", "POST"); Httpresponse.setstatus ($); Httpresponse.setcontentt Ype ("Text/plain;charset=utf-8"); Httpresponse.setcharacterencoding ("Utf-8"); +responsecontentbyte= "{\" test\ ": \" options\ "}". GetBytes (); - } + } A at @Override - Public voidInit (Filterconfig arg0)throwsservletexception { - //System.out.println ("filter Initialization"); - } - -}
The contenttype of Ajax cross-domain is the problem of Application/json request failure