为什么我无法获得ajax请求的响应,而我可以通过正常的浏览器请求得到它?

时间:2015-11-26 15:22:20

标签: javascript jquery html ajax

您好我正在尝试制作一个html文件(不允许PHP),它会向服务器发送ajax请求,然后提醒响应:

$(document).ready(function(){
$("#connect").click(function(){
$.ajax({
url: "http://213.74.86.200:8080/pwr/relays?ac=123456",
type: "GET",
crossDomain: true,
contentType: 'text/plain',
xhrFields: {
withCredentials: false
},
headers: {
    'Access-Control-Allow-Origin': '*'
},
success:function(data,status){
    if(status=="success"){
    alert(data);
    }else{
        alert("Connection Error");
    }
}   
});
});

});

当我用浏览器打开链接时,问题是

http://213.74.86.200:8080/pwr/relays?ac=123456

一切正常,我可以看到响应作为文本但是当我打开我的html文件并单击连接按钮时,我在控制台日志中得到了这个:

OPTIONS http://213.74.86.200:8080/pwr/relays?ac=123456

XMLHttpRequest cannot load http://213.74.86.200:8080/pwr/relays?ac=123456.
Response to preflight request doesn't pass access control check: 
No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'null' is therefore not allowed access. The response had
HTTP status code 405.

请帮忙吗?

1 个答案:

答案 0 :(得分:0)

如错误所示,您需要在Access-Control-Allow-Origin页面上设置relays