Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: XML jQuery JSON Remote
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 29-Mar-13 14:52pm
larssy1367
Edited 29-Mar-13 16:51pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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. Smile | :)
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
  Permalink  
v3
Comments
larssy1 at 29-Mar-13 20:43pm
   
But how could i retrieve the data in the first case??
Sergey Alexandrovich Kryukov at 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 at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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:
 
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:
 
 
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 Smile | :)
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 565
1 Kornfeld Eliyahu Peter 409
2 Maciej Los 369
3 DamithSL 196
4 OriginalGriff 188
0 OriginalGriff 6,353
1 DamithSL 4,854
2 Maciej Los 4,466
3 Kornfeld Eliyahu Peter 4,058
4 Sergey Alexandrovich Kryukov 3,897


Advertise | Privacy | Mobile
Web02 | 2.8.141220.1 | Last Updated 30 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100