SyntaxError:JSON.parse:意外的字符

时间:2017-04-20 22:40:13

标签: javascript json ajax

我正在尝试使用以下代码显示来自skyscanner API的调用响应:

  function ajaxGet(url, callback) {

      var req = new XMLHttpRequest();

      req.open("GET", url);

      req.addEventListener("load", function () {

          if (req.status >= 200 && req.status < 400) {

              callback(req.responseText);
          } else {
              console.error(req.status + " " + req.statusText + " " + url);
          }
      });

      req.addEventListener("error", function () {
          console.error("Erreur réseau avec l'URL " + url);
      });
      req.send(null);
  }


  ajaxGet("http://partners.api.skyscanner.net/apiservices/browseroutes/v1.0/GB/GBP/en-GB/US/anywhere/anytime/anytime?apiKey=mo432604......1342", function (response) {

      var elements = JSON.parse(response)

      elements.forEach(function (ele) {

          console.log(ele);
      })
  });

但我在Firefox控制台中收到此错误:

  

SyntaxError:JSON.parse:JSON数据第1行第1列的意外数据结尾

我检查了API调用对Curl是否有效:

curl http://partners.api.skyscanner.net/apiservices/browseroutes/v1.0/GB/GBP/en-GB/US/anywhere/anytime/anytime?apiKey=mo432604...1342

它有效,因为我收到了这样的回复:

{"Routes":[{"OriginId":1811,"DestinationId":943,"QuoteIds":[1,2],"Price":148.0,"QuoteDateTime":"2017-04-10T02:00:00"},

所以,我不知道我的代码在哪里错了。有什么帮助吗?

由于

0 个答案:

没有答案