Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7762299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:21:52+00:00 2026-06-01T14:21:52+00:00

How would I go around the cross-domain issue when parsing XML from a different

  • 0

How would I go around the cross-domain issue when parsing XML from a different server/domain? Could someone provide me with an example? The example doesn’t have to be restricted to jQuery only, as JavaScript will also suffice.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:21:53+00:00Added an answer on June 1, 2026 at 2:21 pm

    To fully understand why pure cross-domain XML will not work, it helps to first look at how cross domain JSON is facilitated.

    First, let’s look at what happens when you make an AJAX request in jQuery:

    $.ajax({
        url: '/user.php?userId=123',
        success: function(data) {
            alert(data); // alerts the response
        });
    

    In the above example, the AJAX request is made relative to the domain. We know that if we attempt to add a different domain before the path, the request will fail with a security exception.

    However, that’s not to say that the browser cannot make requests to another domain. Here is an example that may be familiar to you:

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    

    Based on our knowledge of how to import JavaScript on the page, we see that it is possible to load a resource that exists on another domain!

    JSONP is a concept that exploits this knowledge. JSONP stands for “JSON with padding”, and it’s success hinges on the fact that JavaScript Objects can be expressed using a string notation, and the fact that JavaScript script tags can load and run content from external domains.

    Under the hood, jQuery’s JSONP looks something like this although it may not be exact:

    // programmatically load a script tag on the page using the given url
    function loadRemoteData(url) {
        var script = document.createElement("script");
        script.setAttribute("type","text/javascript");
        script.setAttribute("src", url);
        document.getElementsByTagName("head")[0].appendChild(script);
    }
    

    Also, on the page somewhere, we define a callback handler:

    function processData(jsonResult) {
        alert(JSON.stringify(jsonResult)); //alert the JSON as a string
    }
    

    Here, we make the request:

    // make a request for the data using the script tag remoting approach.
    loadRemoteData("http://example.com/users.php?userId=123&callback=processData");
    

    For this to work properly, our PHP script must both return the data in JSON format, and it must also add “padding” around the string in the form of a JavaScript function name that we may pass in as a parameter (i.e. “callback”)

    Thus, the response from the server may look something like this, if we were to look at it in the Firebug or Chrome NET tab:

    processData( { "userId" : "123" , "name" : "James" , "email" : "example@example.com" } );
    

    Because we know JavaScript content runs as soon as it’s downloaded, our processData function we defined earlier is immediately called and is passed our JSON string as a parameter. It is then alerted, using JSON.stringify to convert the object back into a string.

    Since it’s an object, I could also access it’s properties, like so:

    function processData(jsonResult) {
        alert(JSON.stringify(jsonResult)); //alert the JSON as a string
    
        // alert the name and email
        alert("User name is " + jsonResult.name + " and email is " + jsonResult.email);
    }
    

    Finally, let’s move onto the main question: Can JSONP be used to fetch XML, or can we parse XML cross-domain? The answer, as others have pointed out, is a resounding NO, but let’s look at why by using an example:

    processData(<?xml version="1.0"><user><userid>12345</userid><name>James</name><email>example@example.com</email></user>);
    

    Now, what will happen if raw XML is passed into the function? It will break, as JavaScript has no way to convert XML into JSON.

    However, suppose we put the XML in quotes:

    processData("<?xml version="1.0"><user><userid>12345</userid><name>James</name><email>example@example.com</email></user>");
    

    Now, in this example, the jsonResult variable actually takes a string, which we can work with. Using some JavaScript XML parsing utilities, we could load that string into the XML DOM Parser and do stuff with it!

    However, it’s not pure XML, it’s still a JavaScript response under the hood. The response type from the PHP server is still text/javascript, and we’re still using a script tag to load what is really just plain JavaScript.

    In summary, you could work with “XMLP” or XML with padding (I just made that up, it’s not real!), but if you’re going to go through all of the trouble of actually modifying your response to return a function callback wrapper, you may as well just convert your output to JSON and let the browser handle conversions automatically and natively and save yourself the trouble of having to use an XML parser.

    But if for some reason it’s easier to keep your data in XML format, you could modify the response and give it a JavaScript wrapper.

    Cases where I could see this being useful might be if you have XML data from a legacy application stored in a database, and you return it to the client-side using script-tag remoting or JSONP calls.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

From the perspective of a cross application/applet java accessibility service, how would you link
What is faster: Function pointers or switch? The switch statement would have around 30
How would I go around overriding a theme function with a .tpl file? I
I'm curious how the Python Ninjas around here would do the following, elegantly and
I would like to move some files around in Visual Studio. But I see
I would like to have fuzzy looking border around my Canvas control. Basically, I
How would I override the tables rendered around the webparts in the Rich Content
I have around 3500 flood control facilities that I would like to represent as
What would be the easiest way to move the mouse around (and possibly click)
I've around 80 instances of this class called Items and would like to efficiently

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.