views:

13

answers:

1

We are trying to create a RESTful API that will be hosted on server x.foo.com. Client html applications (built in jquery) will be hosted on y.foo.com.

I am dealing with cross-domain issues by setting the Access-Control-Allow-Origin header as described here http://www.w3.org/TR/cors/.

So far so good, and I can now successfully make AJAX calls from host y to host x.

However, I ran into a gotcha with POST requests. The typical response to a post request is a redirect. However, the XMLHttpRequest object will not follow cross domain redirects, thus resulting in a failed call.

// Hosted on y.foo.com
$.ajax({
    type: "POST",
    url : http://x.foo.com/myapp/",
    success: function(data) {
      alert("success!");
    }
});

// Return status:  302  
// (Which errors out in firebug)

Anyone know of any techniques to handle the redirect (to a resource on server x) that I get from this post for a client hosted on y?

+1  A: 

How about the client sends a special header for AJAX requests, and depending on whether it's an AJAX request or not, you can change the response instead of doing a redirect.

Anurag
Yes - that might be a possible solution. I might be able to just use the Content-Type header to specify the desired format. If it is JSON I can just send a regular 200 response for a POST.However, I wasn't sure if this was considered a best-practice since most of the REST literature seems to recommend returning a redirect against a POST.
shreddd