[英]Jquery to consume WCF. error: XMLHttpRequest cannot load Origin null is not allowed by Access-Control-Allow-Origin
[英]XMLHttpRequest cannot load http://localhost:8089/jquery. Origin null is not allowed by Access-Control-Allow-Origin
我正在尝试使用chrome从jquery(ajax)中访问Web服务。 但是,当我尝试调用Web服务时,出现以下错误,甚至没有到达服务器
Error :XMLHttpRequest cannot load http://tomohamm-t420:8089/jquery. Origin null is not allowed by Access-Control-Allow-Origin.
下面是我正在使用的一个示例ajax调用://将W / S-Request查询构建为String对象。
var id = '123';
var query = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://www.tibco.com/schemas/jQuerySample/jquery/Schema.xsd"';
query += '<soapenv:Header/';
query += '<soapenv:Body';
query += '<sch:EmpDetailsRequest';
query += '<sch:empID' + id + '</sch:empID';
query += '</sch:EmpDetailsRequest';
query += '</soapenv:Body';
query += '</soapenv:Envelope';
// set end point url..
var endpointUrl = 'http://tomohamm-t420:8089/jquery';
//ajax call to W/S..
$.ajax({
url : endpointUrl,
type : "POST",
beforeSend : function(xhr) {
xhr.setRequestHeader("SOAPAction", "/GetEmpByID");
},
data : query,
dataType : "xml",
contentType : "text/xml; charset=\"utf-8\"",
complete : function(xmldata,stat,response) {
console.log("W/S-Response: "+xmldata.responseText);
},
success : function(data) {
console.log('W/S Successful!');
},
error : function(textStatus, errorThrown) {
console.log('W/S Failed!');
console.log('Error Status :: ' +textStatus);
console.log('Error Message :: ' +errorThrown);
}
});
我找到了避免此问题的方法。 那就是我必须使用以下命令打开chrome:cd C:\\ Program Files(x86)\\ Google \\ Chrome \\ Application Chrome.exe --disable-web-security
但这不能在尝试打开我的应用程序的每台机器上完成。 那么是否有在jquery应用程序中包含此设置,以便我可以直接打开chrome并运行它?
我猜您正在从本地文件进行Ajax调用,对吗? 如果要执行此操作,则需要一些服务器端编码。
在服务器端处理ajax请求并发送响应的服务器端尝试此操作,将Access-Control-Allow-Origin
标头添加到HTTP响应并将值设置为*
。 这是一段Java代码供您参考。
response.setHeader("Access-Control-Allow-Origin", "*")
不。如果您不控制端点,最好的选择是通过自己的服务器代理请求,并添加适当的Access-Control-Allow-Origin标头。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.