2

I've got a short javascript script that I'm running with node (e.g. node runScript.js). Within it, I'm using tiptoe, and I've tried various ways of retrieving an xml file without success.

tiptoe(
  function getESData() {
   var json;
                // get the json data.
         for (var i = 0; i < json.hits.hits.length; i++) {          
          for (var multiId = 0; multiId < json.hits.hits[i]._source.multiverseids.length; multiId++) {
           var priceUrl = "http://partner.tcgplayer.com/x3/phl.asmx/p?pk=TCGTEST&s="+setName+"&p="+json.hits.hits[i]._source.name
     console.log("fetching " +priceUrl );
                  
                    // attempt 1:
     var x = new XMLHttpRequest();
     console.log("working"); // THIS CONSOLE LOG NEVER SHOWS UP.
     x.open("GET", priceUrl, true);
     console.log("working");
     x.onreadystatechange = function() {
      if (x.readyState == 4 && x.status == 200)
      {
       console.log(x.responseXML);
      }
     };
     x.send();
                  
                    // attempt 2:
     $.ajax({
      url: priceUrl,
      success: function( data ) {
        console.log(data);
      }
     });
                  
                    // attempt 3:                  
     $.get(priceUrl, function(data, status){
            console.log("Data: " + data + "\nStatus: " + status);
        });
    }
   }
     });
  }
);

All of these methods fail silently (obviously I comment out all but one when testing, I don't use all three at once), after printing the first console.log where I log the url to make sure it works. (The urls with the variables resolve to something like this: http://partner.tcgplayer.com/x3/phl.asmx/p?pk=TCGTEST&s=Ice Age&p=Arnjlot's Ascent which absolutely returns xml when I test it in a browser, so I know that's working). Is it something with tiptoe?

edit: attempt 4:

    $().ready(function () {
      console.log('working');
   $.get(priceUrl, function (data) {
     console.log(data);
   });
 });

This also fails before the 'working' log shows up in my console. Not sure that it matters, but I'm using a git bash console for this, also.

edit 2: The answer was to use request, in the following way:

request(priceUrl, function(error, response, body) {
  if (!error && response.statusCode == 200) {
    console.log(body);
  }
})

That worked perfectly.

IronWaffleMan
  • 2,513
  • 5
  • 30
  • 59

1 Answers1

1

Yet another Access-Control-Allow-Origin issue. I tried the link you gave:

XMLHttpRequest cannot load http://partner.tcgplayer.com/x3/phl.asmx/p?pk=TCGTEST&s=Ice%20Age&p=Arnjlot%27s%20Ascent. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

There is a well documented post here about the possible solutions.

In your case, you could use jsonp datatype for your request, this only works on jQuery 1.12/2.2+:

var url = "http://partner.tcgplayer.com/x3/phl.asmx/p?pk=TCGTEST&s=Ice Age&p=Arnjlot's Ascent";

$.get({
    url: url,
    dataType: 'jsonp text xml'
}, function(data, status) {
    console.log("Data: " + data + "\nStatus: " + status);
});
Shanoor
  • 13,344
  • 2
  • 29
  • 40
  • That doesn't work either. I run ```node runScript.js```, get my first console log, and then nothing thereafter. I updated my OP with what I used. – IronWaffleMan Apr 14 '16 at 04:37
  • @IronWaffleMan I'm confused, are you using jQuery OR NodeJS? Is it server-side or client-side script? – Shanoor Apr 14 '16 at 04:40
  • I have a single script, called ```runScript.js```, in my local directory. In a console, I navigate there and do ```node runScript.js```. So I believe the answer to your question is both? And I think this qualifies as client-side. – IronWaffleMan Apr 14 '16 at 04:42
  • 1
    No, that qualifies as server-side. A client-side script runs in a browser. If you're using NodeJS, don't use jQuery or XMLHttpRequest for your requests, use something like `request` or (better) `request-promise`. Also, you don't need tiptoe, you should look into Promise.all() (it's a native function of NodeJS). – Shanoor Apr 14 '16 at 04:46
  • Ah, perfect, ```request``` did the trick for me. I will update my OP to clarify the right answer. Thanks :D – IronWaffleMan Apr 14 '16 at 05:03