0

I searched, many answers here. But I tried it all, It still does not work for me. I want to to get json data from web service (cross domain).

var url1 = 'http://localhost:33219/iSes/Pro/RfsPro.svc/GetPro/';
$.getJSON(url1,function(json){
    alert('testing');
});

I got error from Chrome console Origin null is not allowed by Access-Control-Allow-Origin.

These are what I tried to do, by searching the answers here :

  1. Put /?callback=? to url :

    var url1 = 'http://localhost:33219/iSes/Pro/RfsPro.svc/GetPro/?callback=?';
    
  2. I found chrome.exe --allow-file-access-from-files but if we use this, do the clients that browse our website have to do it too?

  3. This answer, 3rd option related to CORS, using PHP to configure the header, How could I do this with ajax or jquery? Because my project is using backbone.jsnot PHP.

Community
  • 1
  • 1
Nothing
  • 2,644
  • 11
  • 64
  • 115
  • Ah, it is alright in my code. Just a typo here. – Nothing Sep 09 '13 at 23:03
  • Are you using the same port and the same url of the origin page? For absolute URIs, the origin is the triple {protocol, host, port}. http://en.wikipedia.org/wiki/Same-origin_policy – Idipaolo Sep 09 '13 at 23:08
  • The service has to send actual JSONP, that means JSON wrapped in a callable function, just calling $.getJSON doesn't make it JSONP, and regular JSON is not supported cross domain unless CORS is enabled, in which case pretty much anything would work. Your best bet is to use $.ajax and set the dataType to JSONP and make sure your service is returning valid JSONP data. – adeneo Sep 09 '13 at 23:08
  • 1. Does your server support JSONP? 2. Nope, it probably is only when you're testing on `localhost`. The issue should be gone after deploying. 3. `backbone.js` is clientside and therefore irrelevant. What software does your server use? – Bergi Sep 09 '13 at 23:08
  • @Idipaolo : yeah I'm testing it with the web service server in my local. But I also try this sample json url `var url5 = 'http://www.bom.gov.au/fwo/IDV60901/IDV60901.94868.json';`, the problem is the same in my local. – Nothing Sep 09 '13 at 23:13
  • @adeneo : I tried this `$.ajax({ url:url1, dataType:'jsonp' });` , the same error occur. – Nothing Sep 09 '13 at 23:17
  • @Bergi : 1. How could I know my server support JSONP or not dear? 2. I'm not yet deploying it, actually I'm trying to do it in my local first. 3. Did you mean, ajax or something else? – Nothing Sep 09 '13 at 23:18
  • @Nothing: It's your server! Read the docs (I assume you haven't written the response logic yourself as you don't know the serverside programming language). – Bergi Sep 09 '13 at 23:19
  • @Bergi : Yeah you're right dear, the json data are returned from the others guys in my team, I just take the URL from them and take data in json format :) – Nothing Sep 09 '13 at 23:23
  • @Nothing: Could you post what is the url of the page that is querying the server,please ? – Idipaolo Sep 09 '13 at 23:23

1 Answers1

1

While the source url and the destination url don't match you will always have this error!

You can't do an ajax request from "file:///E:/Project/WebSite/SourceWebsite/test.html", to "http://yourdomain:33219/iSes/Pro/RfsPro.svc/GetPro/", because it's a violation of the Same Origin Policy.

http://en.wikipedia.org/wiki/Same-origin_policy

You can do an ajax request to the server if and only if the protocol, url and port are the same.

If you want to access to : "http://yourdomain:33219/iSes/Pro/RfsPro.svc/GetPro/"

You must be at "http://yourdomain:33219/something"

I hope I made myself clear.

Idipaolo
  • 788
  • 5
  • 11
  • It is not possible with ajax in HTML5 also? – Nothing Sep 10 '13 at 01:57
  • Yes it is possible in many ways.https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage . Here it is a clear way to do a cross domain request with javascript and html5. – Idipaolo Sep 10 '13 at 07:49