最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - XMLHttpRequest cannot load http:localhost:8089jquery. Origin null is not allowed by Access-Control-Allow-Origin - S

programmeradmin1浏览0评论

I am trying to hit a webservice from jquery(ajax) using chrome. But when i try to invoke webservice i am getting below error and it is not even reaching the server

Error :XMLHttpRequest cannot load http://tomohamm-t420:8089/jquery. Origin null is not allowed by Access-Control-Allow-Origin.

Below is a sample ajax call which i am using: //Build W/S-Request query as String object..

var id = '123';

var query = '<soapenv:Envelope xmlns:soapenv="/" xmlns:sch=".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\"",
plete : 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);
}
});

I found a way to avoid this issue. That is i have to open chrome using the below mand: cd C:\Program Files (x86)\Google\Chrome\Application Chrome.exe --disable-web-security

But this cannot be done on every machine which tries to open my application. So is there anyway to include this setting inside jquery application so that i can directly open chrome and run it?

I am trying to hit a webservice from jquery(ajax) using chrome. But when i try to invoke webservice i am getting below error and it is not even reaching the server

Error :XMLHttpRequest cannot load http://tomohamm-t420:8089/jquery. Origin null is not allowed by Access-Control-Allow-Origin.

Below is a sample ajax call which i am using: //Build W/S-Request query as String object..

var id = '123';

var query = '<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap/soap/envelope/" xmlns:sch="http://www.tibco./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\"",
plete : 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);
}
});

I found a way to avoid this issue. That is i have to open chrome using the below mand: cd C:\Program Files (x86)\Google\Chrome\Application Chrome.exe --disable-web-security

But this cannot be done on every machine which tries to open my application. So is there anyway to include this setting inside jquery application so that i can directly open chrome and run it?

Share Improve this question edited Oct 24, 2013 at 12:04 nathancahill 10.9k11 gold badges55 silver badges93 bronze badges asked Oct 24, 2013 at 11:55 user2773804user2773804 12 silver badges2 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

I guess you are making ajax calls from a local file, right? If you want to do this some server side coding are needed.

Try this in you server side where you process the ajax request and send the response, adding the Access-Control-Allow-Origin header to the HTTP response and set the value to *. Here's a piece of Java code for your information.

response.setHeader("Access-Control-Allow-Origin", "*")

No. If you don't control the endpoint, your best bet is to proxy the request through your own server, and add the appropriate Access-Control-Allow-Origin headers.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论