Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery XML REST Access-Control-Allow-Origin

I am calling Version One REST API and keep getting XMLHttpRequest cannot load https://www10.v1host.com/... Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin. javascript error. I am sending GET request and XML content is expected to be returned in response. This is my jQuery code:

$.ajax({
  dataType: "xml",
  url: "https://www10.v1host.com/...",
  success: function(data, status, xhr) {
  alert("Load was performed.");
  }
});

I have changed dataType: "jsonp" and with this I get response in this format:

jsonp1294354293197(<?xml version="1.0" encoding="UTF-8"?><Asset ...</Asset>)

But get another javascript error: "Uncaught SyntaxError: Unexpected token <"

Is there a way how to get cross domain XML rest data (something like XMLp) or do a workaround for jsonp (prevent parsing JSON format and use my own - parse XML)?

like image 820
padis Avatar asked Jan 06 '11 23:01

padis


2 Answers

You can't do a cross domain XMLHttpRequest, period. As for your own parse XML - that would be only possible if you could get an escaped string instead of bare XML from server. There is no magical parse JSON - JSONP technique just requests another script dynamically adding <script src=...> to header, everything inside is treated as normal JavaScript.

like image 132
Marek Sapota Avatar answered Oct 24 '22 01:10

Marek Sapota


It is not possible as long as the server of the REST API (not your server) allows the request from a different origin by setting the CORS (Cross-Origin Resource Sharing) HTTP header, for example by setting the "Access-Control-Allow-Origin" HTTP header in the response:

Access-Control-Allow-Origin: *

or

Access-Control-Allow-Origin: http://localhost:8080

However what you can do is call your own server with Ajax, then use your own server as a kind of proxy to make a call to the other server in a different domain, parse the XML or HTML result with a suitable parser, and give the result back to the client:

 client --(Ajax)--> server
                    server --(HTTP)---> Site 
                    server <----------
 client <----------
like image 20
0x4a6f4672 Avatar answered Oct 24 '22 01:10

0x4a6f4672