【问题标题】:resteasy ajax request轻松的ajax请求
【发布时间】:2012-12-07 04:21:09
【问题描述】:

我有一个 RestEasy api,我需要向移动客户端提供对所有 http 方法的访问,所有这些请求都必须通过 ajax 发送。

以下是客户端代码示例:

var data = {
   login: 'usertest@gmail.com',
   password: '123456'
 };

 $.ajax({
   url: 'http://1.1.1.3:8080/api/admin',
   type: 'POST',
   contentType : 'application/json',
   dataType: "json",
   data: JSON.stringify(data),
   success: function(){
     console.log(arguments);
   },
   error: function(){
     console.log('error')
   }
 });

这里是服务器:

@POST
@Consumes("application/json")
@Produces("application/json")
public Response doLogin(User user) {...}

这是错误:

XMLHttpRequest cannot load http://1.1.1.3:8080/api/admin. Origin http://stackoverflow.com is not allowed by Access-Control-Allow-Origin.

当我从 Localhost 请求时,这是来自服务器的标头答案

Access-Control-Allow-Cred...    true
Access-Control-Allow-Orig...    http://1.1.1.3:8080 //request sent from http://1.1.1.3:8080
Access-Control-Expose-Hea...    X-Test-2, X-Test-1
Content-Length  1136
Content-Type    text/html;charset=utf-8
Date    Wed, 19 Dec 2012 17:54:19 GMT
Server  Apache-Coyote/1.1

PS:通过通常的 http 请求一切正常。

我错过了什么?

【问题讨论】:

    标签: jquery jax-rs resteasy cors


    【解决方案1】:

    我在朋友的帮助下找到了它...我正在使用 http://software.dzhuvinov.com 此处的 Cors 过滤器库,但我在 web.xml 中缺少一个参数,我修复了将“Origin”添加到:

        <init-param>
            <param-name>cors.supportedHeaders</param-name>
            <param-value>Origin, Accept, Content-Type,
                X-Requested-With</param-value>
        </init-param>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-19
      • 2016-03-30
      • 2020-02-14
      相关资源
      最近更新 更多