views:

2389

answers:

5

With this code http://paulisageek.com/tmp/options.html :

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script>
<script>
$.get("http://metaward.com/import/http://metaward.com/u/ptarjan", function(data) {
     alert(data);
});
</script>

in Firefox 3.5 on Vista it does an OPTIONS request to that url, and then the callback is never called with anything.

When it isn't cross domain, it works fine : http://metaward.com/static/tmp/options.html.

Shouldn't Jquery just make the call with a <script> node and then do the callback when its loaded? I understand that I won't be able to get the result (since it is cross domain), but that's ok, I just want the call to go through. Is this a bug or am I doing something wrong?

EDIT: It seems the OPTIONS is from the Cross-Origin Resource Sharing (CORS) standard. See http://metajack.im/2010/01/19/crossdomain-ajax-for-xmpp-http-binding-made-easy/ about allowing cross domain requests.

+1  A: 

try with:

$.get("http://metaward.com/import/http://metaward.com/u/ptarjan",{},function(data){alert(data);});

Adrián
same thing. also same if the 4th param is "text"
Paul Tarjan
+1  A: 

It's looking like Firefox and Opera (tested on mac as well) don't like the cross domainness of this (but Safari is fine with it).

You might have to call a local server side code to curl the remote page.

contagious
+2  A: 

In fact, cross-domain AJAX (XMLHttp) requests are not allowed because of security reasons (think about fetching a "restricted" webpage from the client-side and sending it back to the server – this would be a security issue).

The only workaround are callbacks. This is: creating a new script object and pointing the src to the end-side JavaScript, which is a callback with JSON values (myFunction({data}), myFunction is a function which does something with the data (for example, storing it in a variable).

Adrián
right, but I can load it in a <script src=""> or <img src=""> and the browser will happily hit it. I just want to know when it is fully loaded so i can query for the result of the import.
Paul Tarjan
+1  A: 

I don't believe jQuery will just naturally do a JSONP request when given a URL like that. It will, however, do a JSONP request when you tell it what argument to use for a callback:

$.get("http://metaward.com/import/http://metaward.com/u/ptarjan?jsoncallback=?", function(data) {
     alert(data);
});

It's entirely up to the receiving script to make use of that argument (which doesn't have to be called "jsoncallback"), so in this case the function will never be called. But, since you stated you just want the script at metaward.com to execute, that would make it.

VoteyDisciple
would MY callback still be notified that the script element has fully loaded? I just want to make sure the update happened before I query the API for it.
Paul Tarjan
You will if the receiving script supports JSONP, and is willing to call the function you identify. If the script does nothing but generate a block of JSON data with no other behavior, you won't be able to tell when it's done loading.If it's essential to tell when it's done loading, you might consider implementing a script on your own server that acts as a proxy.
VoteyDisciple
+2  A: 

I didn't need the output of the call, so I actually solved this problem with an image beacon instead of ajax call.

<img src="http://metaward.com/import/http://metaward.com/u/ptarjan" />

The OPTIONS is from http://www.w3.org/TR/cors/ See http://metajack.im/2010/01/19/crossdomain-ajax-for-xmpp-http-binding-made-easy/ for a bit more info

Paul Tarjan