我使用的是Spring 3.2.2和jQuery。Spring和jQuery POST跨域
一切工作正常在本地。我做了一個CORS過濾器,這也很好。
從另一個主機調用的每個服務方法都在工作,除了一個以外。實際上,這是在客戶端使用@RequestBody
和$.toJSON
/contentType
的唯一方法。
彈簧側:
@RequestMapping(value = "/requestOrder", method = RequestMethod.POST)
@ResponseBody
public Object requestOrder(@RequestBody OrderViewModel order) throws NamingException {
...
}
jQuery的一面:
$.ajax({
type : 'POST',
url : serviceUrl + 'requestOrder',
crossDomain : true,
data : $.toJSON({ ...orderdata... }),
contentType : 'application/json; charset=UTF-8'
});
Chrome的控制檯日誌:
XMLHttpRequest cannot load https://REMOTE_HOST/project/service/requestOrder. The request was redirected to 'https://REMOTE_HOST/project/;jsessionid=84781b083f5305ffa22a0adae0a6', which is disallowed for cross-origin requests that require preflight.
HTTP標頭:
Request URL:https://.../requestOrder
Request Method:OPTIONS
Status Code:302 Moved Temporarily
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Access-Control-Request-Headers:accept, content-type
Access-Control-Request-Method:POST
Cache-Control:no-cache
Connection:keep-alive
Host:...
Origin:http://...
Pragma:no-cache
Referer:http://...
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.102 Safari/537.36
我沒有看到Content-Type
應該設置...請求方法是OPTIONS
?!
這讓我覺得,這個問題是從春天來但這是本地實際工作...
我會很高興得到一些幫助。
你能後重定向的完整URL對錯誤信息? – renanlf
其實不是正確的,但我會重命名。 –
嘗試在您的RequestMapping中添加produce =「application/json」 – renanlf