tags:

views:

64

answers:

2

Hi everybody here, I want to use mootools and SqueezBox class to handle a request to a RESTful service. I don't want to use any server-side script. I am using AJAX. I send a request to the following url using GET method. http://www.idevcenter.com/api/v1/links/links-upcoming.json but I receive a 404 error. Is it because cross-site scripting? here is my code:

SqueezeBox.initialize({handler:'url',ajaxOptions:{method:'GET'}});
$('a.modal').addEvent('click',function(e){
    new Event(e).stop();
    SqueezeBox.fromElement($('a.modal'));
});

In Firebug console, sometimes 'aborted' is shown and sometimes '404'.what is wrong with that?

A: 

You cannot use XMLHttpRequest (that is, ordinary "ajax") to call a service on a server that is not in your domain.

You can, however, use the JSONP trick, which takes advantage of the fact that the browser will load Javascript from other domains. However, the service has to know that you're going to do that, and it has to understand the protocol. That particular service seems perfectly willing to give me a JSON response, but it doesn't pay attention when I give it a "callback" parameter. (I've tried both "callback" and "jsonp" and the JSON blob that comes back is the same, without a function call wrapper.)

Pointy
Thank you for your attention. if it is not possible to send requests, why I get response with 404 error? I think if no call is made, I shouldn't get response from server.
Morteza M.
+2  A: 

XMLHttpRequest is subject to the Same Origin Policy; if the document your JavaScript is running within is not from the same origin as the service you're trying to call, the call will be disallowed for security reasons.

There is now a proposed standard for cross-origin resource sharing to address this. It may be that the service you're trying to use supports it; if so, using a browser that implements CORS (recent versions of Firefox and Chrome do, as do some others) may work. IE8 supports it but requires that you do extra work.

T.J. Crowder
I see some headers in my request which seems to be cross-origin headers. and I get the response, but with 404 error. so I think call is made.Access-Control-Request-Method: GETAccess-Control-Request-Headers: x-requested-with
Morteza M.
Yes, both of those are CORS request headers (there should be an `Origin` as well). If you're getting a 404 (I missed that in your original question!), that *should* mean that the resource isn't at that URL at all, not that you don't have access to it (that's what 401, 403, and the like are for - http://en.wikipedia.org/wiki/List_of_HTTP_status_codes). So I'd check the URL again.
T.J. Crowder
Update: I just tried it, and I get the same result: A direct browser request gives me the JSON, but I can't load it via XHR. The reason is that idevcenter is responding to the OPTIONS request with a 404, which means they don't support CORS (or at least, not pre-flighted CORS). I would have expected something other than 404, but hey, CORS is new. It may be worth asking them if they would consider it.
T.J. Crowder
hey, thank you!! Your answer was really helpful. I sent an email for them. maybe they answer;)
Morteza M.