views:

46

answers:

2

I am providing web service which return data as JSON object. The problem is with Ajax, Ajax can't call cross domain url. Is it possible to disable it?

+3  A: 

You can't disable it, but you can solve the problem by accepting JSONP-requests.

Magnar
A: 

Use JSONP if you can control what the other server responds. JSONP has to return a javascript compliant script. (var hi = {json = stuff};)

Example for the client HTML:

// This is our function to be called with JSON data
function showPrice(data) { 
    alert("Symbol: " + data.symbol + ", Price: " + data.price);
}
var url = “ticker.js”; // URL of the external script
// this shows dynamic script insertion
var script = document.createElement('script');
script.setAttribute('src', url);

// load the script
document.getElementsByTagName('head')[0].appendChild(script); 

In this case the "ticket.js" url contains something like this:

var data = {symbol: 'Hi', price: 91.42};

Possibility two is you create a local (php, perl, ruby, whatever you use) script which proxies to the external JSON source.

sinni800
This isn't entirely correct. The server should wrap the JSON-data in a function callback.
Magnar
So, how should it look then?
sinni800