开发者

How to disable Cross Domain Restriction

开发者 https://www.devze.com 2023-01-18 09:19 出处:网络
I am providing web service which return data as JSON object. T开发者_JAVA百科he problem is with Ajax, Ajax can\'t call cross domain url. Is it possible to disable it?You can\'t disable it, but you can

I am providing web service which return data as JSON object. T开发者_JAVA百科he problem is with Ajax, Ajax can't call cross domain url. Is it possible to disable it?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消