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.

2
  • url encode your string man Commented Apr 14, 2016 at 4:04
  • I guess the problem is with your for loops. Bringing the tiptoe inside your for loops would do the task for you, as tip toe executes each function in serial sequence one after the other. Commented Apr 14, 2016 at 4:11

1 Answer 1

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);
});
Sign up to request clarification or add additional context in comments.

5 Comments

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 I'm confused, are you using jQuery OR NodeJS? Is it server-side or client-side script?
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.
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).
Ah, perfect, request did the trick for me. I will update my OP to clarify the right answer. Thanks :D

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.