节点js脚本与ajax的XML请求不工作

我有一个简短的JavaScript脚本,我正在与节点(例如node runScript.js )运行。 其中,我使用脚尖 ,我已经尝试了各种方式检索一个XML文件没有成功。

 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); }); } } }); } ); 

所有这些方法在默认情况下都失败了(当然,除了一个testing以外,我不会一次性全部使用这三个),在打印第一个控制台日志之后,确保它可以正常工作。 (具有variables的urlparsing为如下所示: http://partner.tcgplayer.com/x3/phl.asmx/p?pk=TCGTEST&s=Ice Age&p=Arnjlot's Ascent : http://partner.tcgplayer.com/x3/phl.asmx/p?pk=TCGTEST&s=Ice Age&p=Arnjlot's Ascent ,当我在浏览器中testing时绝对返回xml ,所以我知道这是工作)。 tip手something脚吗?

编辑:尝试4:

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

在工作日志出现在我的控制台之前,这也失败了。 不知道这个问题,但是我也使用了git bash控制台。

编辑2:答案是使用request ,按以下方式:

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

这工作完美。

另一个访问控制 – 允许来源问题。 我尝试了你给的链接:

XMLHttpRequest无法加载http://partner.tcgplayer.com/x3/phl.asmx/p?pk=TCGTEST&s=Ice%20Age&p=Arnjlot%27s%20Ascent 。 请求的资源上没有“Access-Control-Allow-Origin”标题。 原因'null'因此不被允许访问。

这里有一个很好的文件可能的解决scheme。

在你的情况下,你可以使用jsonp数据types的请求, 这只适用于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); });