【问题标题】:Spring and jQuery POST crossdomainSpring 和 jQuery POST 跨域
【发布时间】:2014-01-31 12:44:49
【问题描述】:

我正在使用 Spring 3.2.2 和 jQuery。

在本地一切正常。我制作了一个 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 ?!

这让我认为问题来自 Spring,但这实际上在本地工作......

我很乐意得到一些帮助。

【问题讨论】:

  • 您能否在错误消息中发布完整的重定向 URL?
  • 其实不是正确的,但我会重命名。
  • 尝试将produce="application/json" 添加到您的RequestMapping
  • 如果是跨域 JSON,您可能需要使用 JSONP。在这里查看:stackoverflow.com/questions/2067472/what-is-jsonp-all-about
  • produces="application/json" 已经在控制器上 - 抱歉我没有提到它。打算用 JSONP 试试。

标签: java javascript jquery spring spring-mvc


【解决方案1】:

看来您需要使用 JSONP,如果此请求正在发送到另一个域。

检查这里: What is JSONP all about?

【讨论】:

  • 感谢您的建议,但实际上,我已经在使用 CORS。
  • 使用 JSONP,服务器会抛出一个 415 Unsupported Media Type :(
【解决方案2】:

您缺少内容类型映射。试试这个

@RequestMapping(value = "/requestOrder", method = RequestMethod.POST, ,headers="Content-Type=application/json")

对于 JQuery 部分 看起来你需要同时设置

 contentType: 'application/json; charset=utf-8',
    dataType: 'json',

Send JSON data with jQuery

【讨论】:

  • 不工作。实际上,标头 Content-Type 甚至不是由 jQuery 发送的。
【解决方案3】:

找到了解决办法:

我将 Spring mvc 方法的参数更改为 StringRequestBody 更改为 RequestParam

@RequestMapping(value = "/requestOrder", method = RequestMethod.POST)
@ResponseBody
public Object requestOrder(@RequestParam("order") String orderJson) {

Ajax 调用现在是这样的:

$.ajax({
    type : 'POST',
    url : serviceUrl + 'requestOrder',
    crossDomain : true,
    data : { order : $.toJSON({ ...orderdata... }) },
    contentType : 'application/json; charset=UTF-8'
});

requestOrder 方法中,我手动解析json:

ObjectMapper mapper = new ObjectMapper();
OrderViewModel order = mapper.readValue(orderJson, OrderViewModel.class);

【讨论】:

    猜你喜欢
    • 2011-08-19
    • 2014-11-18
    • 2011-07-06
    • 2011-10-09
    • 2012-06-05
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2015-02-03
    相关资源
    最近更新 更多