1

What should I Change the below code to load XML cross domain using jsonp..

$.ajax({
    type: "GET",
    url: "http://www.w3schools.com/xml/note.xml",
    dataType: "xml",
    success: function(xml) {
        alert('Hi');
    }
});

Hope my problem is resolved.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
  • Does w3schools support JSONP? Or is this just an example. – Felix Kling Dec 01 '11 at 21:09
  • That is just an example.. I will have a XML file in my FTP and I want to load it to one of my web application that is hosted in another domain... –  Dec 01 '11 at 21:11
  • For people stopping by. Read this to have an idea how cross domain javascript calls work http://stackoverflow.com/a/11736771/228656 – Abdul Munim Aug 02 '12 at 09:30

4 Answers4

3

You cannot load XML with jsonp, because data have to be written in json in a certain way.

Let's assume that your current data is something like that:

<address>
  <fullname>John Doe</fullname>
  <street>1st street</street>
  <number>345</number>
  <zip>12345</zip>
  <city>Nowhere</city>
</address>

You'll have to send it in JSON, something like that:

{
  fullname: "John Doe",
  street: "1st street",
  number: 345,
  zip: "12345",
  city: "Nowhere"
}

Moreover, if you need to receive it through JSONP, you'll need to make another modification. Let's say that you're sending your request like that:

$.ajax({
  type: "GET",
  url: "http://www.w3schools.com/json/note.js",
  dataType: "jsonp",
  success: function(data) {
    alert('Hi');
  }
});

When calling the web service, jQuery will add a parameter named callback in the request URL.

Let's say the generated URL is: http://www.w3schools.com/json/note.js?callback=callback1234

Then, your json output will need to look like this:

callback1234({
  fullname: "John Doe",
  street: "1st street",
  number: 345,
  zip: "12345",
  city: "Nowhere"
});
ghusse
  • 3,200
  • 2
  • 22
  • 30
1

Cross Domain using JSONP is not possible in some scenarios. Please find the below URL for Cross Domain AJAX Request using Jquery directly or using XDomainRequest object http://rajendrapathi.webs.com/apps/forums/show/14007722-jquery-and-javascript

Rajendra
  • 11
  • 1
1

As far as I know you can't (directly) load XML data using JSONP.

Cross-site AJAX with JSONP relies on wrapping the required object inside a Javascript function call that's executed inside a dynamically created <script> tag, and there's no mechanism for doing that with XML.

Even JSONP requires that the remote server perform the JSON wrapping - a CGI script outputing JSON data doesn't automatically support JSONP out of the box.

That's clearly going to be impossible if your data is actually plain XML files sat on an standard FTP server.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
0

I think dataType should be JSONP if you want to receive JSONP.

http://api.jquery.com/jQuery.ajax/

Filip
  • 2,514
  • 17
  • 28