0

I've started to work on a private project. However, in the first stage, I am already facing a problem.

  1. The returned data is in XML format (not that bad, but takes a lot of effort to parse to usable objects)
  2. 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 that there is no alternative for the jsonp argument when using $.ajax, and the name $.getJSON is as it says, meant to retrieve JSON, not XML.

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


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 :']


Solved. Please refer to the following post: $.getJSON or $.ajax alternative for XML retrieval

Community
  • 1
  • 1
lt.kraken
  • 1,287
  • 3
  • 10
  • 27
  • Among other things, look into [CORS](https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS) – Boaz Mar 29 '13 at 23:46
  • If the response is only in XML you might consider a solution in which you create a PHP file that executes a request using cURL and changes the XML reply of the original script to a json response which can be handled by your application. So: `xml.php` <- curl -> `json.php` <- jquery -> `$.ajax` – Joshua - Pendo Mar 29 '13 at 23:46
  • Do you control the remote server to where you can adjust the CORS policy? If not, are you are legitimate consumer of that data such that you can ask that service to allow you in their CORS policy? – Mike Brant Mar 29 '13 at 23:50
  • @Boaz When looking in all the examples given on their website, i'm noticing the ORIGIN property. As long this is there, it wont work. – lt.kraken Mar 29 '13 at 23:56
  • @PENDO That seems like a lot of work for a simple task. Are you sure there is no other alternative? – lt.kraken Mar 29 '13 at 23:57
  • @MikeBrant No, the remote server is not under my control. The company does however provide an API which can be accessed when given an propery keyId and code. – lt.kraken Mar 29 '13 at 23:59
  • XML to JSON convert: http://stackoverflow.com/questions/4079352/jquery-plugin-for-serializing-xml-to-json / http://stackoverflow.com/questions/1773550/xml-json-conversion-in-javascript / http://stackoverflow.com/questions/3642789/how-to-convert-xml-to-json-using-jquery (this should tackle your first problem) – Joshua - Pendo Mar 30 '13 at 00:08
  • Already found that PENDO, but thx :P But that XML is returned is still a problem.. simply takes more converting time :P The problem is mainly number 2. – lt.kraken Mar 30 '13 at 00:15
  • possible duplicate of [Ways to circumvent the same-origin policy](http://stackoverflow.com/questions/3076414/ways-to-circumvent-the-same-origin-policy) – Quentin Mar 30 '13 at 09:15

1 Answers1

0

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:

<?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

lt.kraken
  • 1,287
  • 3
  • 10
  • 27