gpt4 book ai didi

jquery - 轻松的ajax请求

转载 作者:行者123 更新时间:2023-12-01 01:49:24 24 4
gpt4 key购买 nike

我有一个 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.

这是我从本地主机请求时来自服务器的 header 答案

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 请求一切正常。

我错过了什么?

最佳答案

我在 friend 的帮助下找到了它...我正在使用这里的 Cors Filter 库 http://software.dzhuvinov.com我在 web.xml 中缺少一个参数,我修复了添加“Origin”到:

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

关于jquery - 轻松的ajax请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13958757/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com