Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

How to make synchronous ajax call in cross site domain

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!

 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Sheriff
Posts: 67651
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic