Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
I've started to work on a private project. However, in the first stage already faced a problem.

The returned data is in XML format (not that bad, but takes alot of effort to parse to usable objects)
The remote server doesn't accept any ORIGIN except for himself, so the browser throws the error: Origin * is not allowed by Access-Control-Allow-Origin.

the (*) could either be null, localhost, or my website.
So I have been searching, but unable to find any results.

It appears there is no alternative for the jsonp argument when using $.ajax, and the name $.getJSON is as it says, ment to retrieve JSON, not XML.

So i'm wondering, it there any other alternative method to retrieve the external XML data while disabling the ORIGIN property when sending the request?

Yours Sincerley, Lars
----

I've noticed it is possible to obtain the contents with the PHP function: file_get_contents. But I'd really like to know if there is a different way using JQuery.

And if anyone knows how I could let JQuery communicate with the returned PHP file contents, please feel free to enlight me :']
Posted
Updated 29-Mar-13 15:51pm
v2

The methods $.ajax, even less $.getJSON are unrelated to XML. You use Ajax to download some data, then post it as XML, as you correctly put it.

Hard or not the parsing is, you have to do it; "usable objects" don't parse themselves. :-)
Here is how you do it with jQuery: http://api.jquery.com/jQuery.parseXML/[^].

[EDIT]

The code sample in the article generally illustrates how to use XML. This is a bit more detailed example:
http://tech.pro/tutorial/877/xml-parsing-with-jquery[^].

By the way, this is the plug-in to convert XML to JSON: http://www.fyneworks.com/jquery/xml-to-json/[^].

—SA
 
Share this answer
 
v3
Comments
larssy1 29-Mar-13 20:43pm    
But how could i retrieve the data in the first case??
Sergey Alexandrovich Kryukov 29-Mar-13 22:37pm    
Using Ajax. What exactly is not clear? The data send via Ajax depends on requirement of the service/site...
—SA
larssy1 30-Mar-13 4:37am    
Well.. what is not clear.. is how I could retrieve the data from the remote server, but remove the Origin headers from the request.

For JSON, the jsonp dataType is the solution.
What is the solution for XML?
Sergey Alexandrovich Kryukov 30-Mar-13 21:28pm    
I don't understand the point with removing the headers...
I added couple of references to the answer, please see after [EDIT].
—SA
This is not an actual solution for my problem given. But it is a way to bypass the origin using PHP.

In my javascript, I've requested my PHP code using the following:

JavaScript
function Communication () {

    this.global = new Global();
    
    this.allowedParameters = ["Character"];

    this.retrieveXMLFromRemoteServer = function (section, keyId, vCode) {
        if (this.global.arrayContainsValue(section, this.allowedParameters)) {
            $.get(this.formatCommunicationURL(section, keyId, vCode), function(data) {
                alert(data);
            });
        }
    }

    this.formatCommunicationURL = function (baseUri, keyId, vCode) {
        return "server/" + baseUri + ".php?keyId=" + keyId + "&vCode=" + vCode;
    }
}


Whichs called for example: server/Character.php?keyId=00001&vCode=002342345234

The PHP file contains the following code:

PHP
namespace my_api;


class Character {
    function retrieveCharacters ($keyId, $vCode) {
        return file_get_contents("https://external_url/Characters.aspx?keyId=" . $keyId . "&vCode=" . $vCode);
    }
}

$char = new Character();
echo $char->retrieveCharacters($_GET['keyId'], $_GET['vCode']);

?>


This file retrieves the data from the external URL, and then echo's the output so there is a response text JS will be able to use.

Hope this helps for some of you :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900