jroesch
jroesch

Reputation: 1260

Making RESTful requests Cross-Domain in Javascript

I'm trying to find the most convenient, and efficient(i.e. scalable) way to make cross-domain requests in Javascript, that allows use of full HTTP verbs(GET, POST, PUT, DELETE) in my requests. Our issue is we have a light weight architecture that allows for data storage and and an API to be distributed across multiple small servers, and to have either one, or two servers providing web services mostly written in pure Javascript.

I want to be able to consume the API from Javascript with the least ugly hack/solution, I've played with JSONP, server side proxying(not sure if scalable?) and iframe proxying, but these all end in needing a special functionality hosted by the API to translate these requests to normal calls. Is there a way for me to get this functionality without annoying work arounds, or am I stuck with one?

(I also looked at postMessage, but its still not fully supported and provides the same issue of not being able to specify the HTTP verb.)

Upvotes: 1

Views: 1822

Answers (1)

abraham
abraham

Reputation: 47833

What you want is CORS but the browser support is still fairly limited. MDC has the best documentation covering Access-Control-Allow-Origin headers.

The basic flow is the browser does a pre-flight OPTIONS request, the server responds with headers allowing access, and the browser makes the full request.

Upvotes: 2

Related Questions