0

I am new to jquery and don't know to fetch json data from another domain(Cross domain).

function createCORSRequest(method, url){
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr){
    xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined"){
    xhr = new XDomainRequest();
    xhr.open(method, url);
} else {
    xhr = null;
}
return xhr;
}

var request = createCORSRequest("get", "http://www.stackoverflow.com/");
if (request){
request.onload = function() {
    // ...
};
request.onreadystatechange = handler;
request.send();
}

I found this program from here Ways to circumvent the same-origin policy

This is saying by using above code we can access cross domain json data.

I copied the code. This is saying handler is undefined

I don't know how to define handler .

I also don't know what to write inside request.onload

where I will get the json result

Please help

Thanks in advance

Community
  • 1
  • 1
bhagirathi
  • 521
  • 6
  • 23

1 Answers1

2

The handler is a function

it should be something like

function handler(){
   var response = xhr.responseText;
   // do more with your response.
}

Also you xhr should be defined outside of the function createCORSrequest.

See docs on XDR

I know you said you are new to jquery but you should also look into $.getJSON. Its much easier.

Praneeta
  • 1,554
  • 3
  • 19
  • 20
  • Hi Praneeta I am trying to access other domain data. but it is showing error XMLHttpRequest cannot load http://192.168.10.13:8080. Origin http://localhost is not allowed by Access-Control-Allow-Origin. how to solve this problem. – bhagirathi Oct 18 '12 at 05:56
  • You will have to add 'Response.AppendHeader("Access-Control-Allow-Origin", "*");' to your server side code.found it on this page [http://stackoverflow.com/q/6516591/1028488](http://stackoverflow.com/q/6516591/1028488) $.getJson can be used for accessing data from different domain(I use it all the time), but I am not sure how IE treats it. – Praneeta Oct 18 '12 at 13:48
  • Also once you are done testing on localhost & move it to a server, you can chnage th access control from * to you domain name (security) – Praneeta Oct 18 '12 at 13:51
  • Also I found jQuery.XDomainRequest.js (https://github.com/MoonScript/jQuery-ajaxTransport-XDomainRequest) very helpful – Praneeta Oct 18 '12 at 15:52
  • [Click Here](http://192.168.10.113:8080/solr/select/?q=asp.net%20session%20management&indent=on&hl=true&hl.fl=id,name&wt=json&fl=*,score) This is my data I want to fetch. I tried all possible way but did't succeed . please help to retrieve data from this URL. – bhagirathi Oct 19 '12 at 07:05
  • Your server is probably on a private network. I cannnot open the link you sent. Send me an email with a sample of how your data looks. Also please try this code, ` jQuery.ajax({ url: yourUrl, dataType: 'json', success: function (data) { alert('Success'); }, error: function (data) { alert('Fail'); } }); ` If you are using chrome, please check the network tab of your inspector(debugging tool) & make sure if you are getting a response. – Praneeta Oct 19 '12 at 13:38
  • Praneeta Can you please share your email id so that i can send the data. – bhagirathi Oct 22 '12 at 05:49
  • Thanks Praneeta for your support . Solved that problem . your suggestion helped me a lot. Thanks – bhagirathi Oct 23 '12 at 06:09