0

So i have a simple task, get an xml feed from a server that is not ours. easy enough, however im running into allow origin control issues and the &callback= tag isnt solving the issue.

$.get("http://www.buytopia.ca/feed",data,jloop(),"xml");

function jloop(){
        var count=0;
        //dummy code
        document.write("please work");
        do{
            document.write(count);
            count++;
        }
        while (count<10);

    };

so when i run this, the get call is pending, not retieving. ive tried different feeds, like NASA but adding the &callback= doesnt remove access origin problems. All i need is a working chunk of code to get the feed, preferably to that buytopia.ca feed because we have access to it and know its permission info. Then i can begin to parse it. Any help would be great! thanks!

4
  • &callback= is for JSONP, not XML.
    – Barmar
    Commented Jul 11, 2013 at 16:46
  • 1
    so am i stuck making a proxy then?
    – Japes
    Commented Jul 11, 2013 at 16:47
  • Yes, that's probably right.
    – Barmar
    Commented Jul 11, 2013 at 16:47
  • lame. seems like such an easy thing to do. but its been hell. if the feed is public, is there no easier way to grab it?
    – Japes
    Commented Jul 11, 2013 at 16:52

2 Answers 2

0

You can do an ajax call to a local php file (let's say you call it currency.php), and get it through php as in, for example (the php is from ECB's web site, for developers - the ajax is mine)

  $XML=simplexml_load_file("http://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml");

  //the file is updated daily between 2.15 p.m. and 3.00 p.m. CET 

    foreach($XML->Cube->Cube->Cube as $rate){
        //Output the value of 1EUR for a currency code
        echo '1&euro;='.$rate["rate"].' '.$rate["currency"].'<br/>';

        if ($rate["currency"]=='USD') {
            echo 'EUR-> USD RATE: '.$rate["rate"], '<br/>';
        }
        //--------------------------------------------------
        //Here you can add your code for inserting
        //$rate["rate"] and $rate["currency"] into your database
        //--------------------------------------------------
    }

Then the ajax call would be something like:

<script>
 $.ajax({
        url: 'currency.php',
        type: 'POST',
        data: {myCurrency: curr,
               myPriceAmt: amount },  
        success: function(data){
            // do any manipulations here

        }
 });
 </script> 

Good luck!

0

Is buytopia your domain. You cant make ajax calls accross domains. Its called cross site scripting.

https://en.wikipedia.org/wiki/Cross-site_scripting

7
  • no it is not my domain. its from another project a college has access to.
    – Japes
    Commented Jul 11, 2013 at 16:46
  • This is a security risk and most browsers will not allow you to make ajax calls across domains. You could create a proxy in your own domain. Call a service in your domain that on server side makes a call to the third party (buytopia) and then returns the XML.
    – Thomas
    Commented Jul 11, 2013 at 16:48
  • yes i am aware of this. let me put it to you this way. my computer right now can view an xml feed. it does it over http. so isnt their a way to do that in code and parse it? im running this locally at the moemnt
    – Japes
    Commented Jul 11, 2013 at 16:49
  • What do you mean your computer can read an xml feed? Sure you can make and http request to buytopia.ca/feed but thats not really the issue. You are trying to inject xml via javascript. The browser does not let you inject content from another domain via javascript because its a security issue. Again you can read the Wikipedia article.
    – Thomas
    Commented Jul 11, 2013 at 16:56
  • ok, well would switching to JSON (which seems simple though ive never used it before) be a solution to this? much thanks for your replies as well XD
    – Japes
    Commented Jul 11, 2013 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.