This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes HTML, CSS and JavaScript and the fly likes How to make synchronous ajax call in cross site domain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "How to make synchronous ajax call in cross site domain " Watch "How to make synchronous ajax call in cross site domain " New topic
Author

How to make synchronous ajax call in cross site domain

soni lal
Greenhorn

Joined: Jan 31, 2011
Posts: 29
Hi All,

I have to make one synchronous ajax call which solves cross site domain issue.

The ajax call using window.XMLHttpRequest throws error while making call in cross domain.

The ajax call using JSONscriptRequest supports only asynchronous ajax call.

I tried in third way. But it is also not be bale to make synchronous ajax call. Please find the code below:

url = url + '&callback=' + "ACFeedStatistics.callbackSuccess";
var head = document.getElementsByTagName('head')[0];
script = document.createElement('script');
script.id = 'xdShortcutContainer';
script.type = 'text/javascript';
script.src = url;
head.appendChild(script);


I am totally stuck.

Please help me.

Thanks in advance!!

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The same origin policy prevents it. It is to keep sites from contacting your bank, email, etc.

If the external site is under your control and you only care about modern day browsers, you can look into CORS.

If the site is not, only way around it is a serverside proxy on your server to fetch the content.

In reality, synchronous calls are a bad idea since they lock up the browser.

Eric


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

I agree with Eric. Unless you are able to write a 14-page justification for why the call should be synchronous, it should not be.

And as Eric suggested, you can use a server-side relay to perform the remote request. Or if the remote server supports JSONP, you can make the request directly.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How to make synchronous ajax call in cross site domain