无法使用AJAX从Server获取JSON文件

时间:2015-01-29 21:47:06

标签: javascript ajax json

我试图通过AJAX向用户选择的停止号码发送请求到公交车到达时间API网址,该网址返回一个* .json文件给我,我需要能够在我的代码中使用。当我运行该程序时,它会遇到错误'在我的getJSON函数中。当我通过浏览器查看提琴手甚至调试时,我可以看到它返回了我想要使用的数组。在,I.E。或Firefox,我可以查看响应/响应主体并在那里看到数组。专家们,不知道我能在哪里或为什么能让它发挥作用,所以我向所有人求助。请让我知道我还能尝试和提供什么。

使用此库:



<script src="js/jquery-1.11.2.js"></script>
&#13;
&#13;
&#13;

// Returns the next 4 bus arrival times  
function getTimes(stopNumber) {

var busArrivalAPI = "http://cfsrv1/api/index.cfm/rtcTimes/"+ stopNumber;

function getJSON() {

  $.ajax({
    type: 'GET',
    url: busArrivalAPI,
    dataType: "jsonp",
    contentType: "application/json; charset=utf-8",          
    success: function(data) {
      busArrival = $.parseJSON(data);
      // This will remove old values so table will only load current Times
      var results = document.getElementById("results");
      var rowCount = results.rows.length;
      for (var x=rowCount-1; x>0; x--) {
        results.deleteRow(x);
      }

      //  This will populate the result table with the correct bus routes/times/direction
      data.forEach(function(busArrival) {
        var tr = document.createElement('tr');
        var route = document.createElement('td');
        route.appendChild(document.createTextNode(busArrival.ROUTE));
        var arrival = document.createElement('td');
        arrival.appendChild(document.createTextNode(busArrival.ARRIVAL));
        var direction = document.createElement('td');
        direction.appendChild(document.createTextNode(busArrival.DIR));
        tr.appendChild(route);
        tr.appendChild(arrival);
        tr.appendChild(direction);
        document.getElementById('results').appendChild(tr);
      });
    },
    error: function() {
      alert(jqXHR, textStatus, errorThrown);
    }
  });
}
getJSON();
}

来自Fiddler Web调试器的原始数据(我只需要ROUTE,ARRIVAL和DIR):

GET http://cfsrv1/api/index.cfm/rtcTimes/2935?   callback=jQuery111206901489450284122_1422567353037&_=1422567353038 HTTP/1.1
Accept: application/javascript, */*;q=0.8
Referer: http://127.0.0.1:8020/RTCSystemMap/index.html
Accept-Language: en-US
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like     Gecko
Accept-Encoding: gzip, deflate
Host: cfsrv1
Connection: Keep-Alive


HTTP/1.1 200 OK
Connection: close
Date: Thu, 29 Jan 2015 21:35:58 GMT
Server: Microsoft-IIS/6.0
MicrosoftOfficeWebServer: 5.0_Pub
X-Powered-By: ASP.NET
X-RTCTimes-HEADER: RTCSNV Times
Content-Type: application/json; charset=utf-8
ALLOW: GET

[{"ARRIVAL":"01:44P","DEV":"   0","ROUTE":"104","DIR":"N","TRIPID":"357498261"},
{"ARRIVAL":"02:14P","DEV":"  10","ROUTE":"104","DIR":"N","TRIPID":"357498262"},
{"ARRIVAL":"02:44P","DEV":"   1","ROUTE":"104","DIR":"N","TRIPID":"357498263"},
{"ARRIVAL":"03:14P","DEV":"   0","ROUTE":"104","DIR":"N","TRIPID":"357498264"}]

&#13;
&#13;
<div style="overflow-x:scroll; overflow-y:scroll;" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="title:'Bus Arrival Times', selected:true">
  <table id='results'>
    <tr>
      <th><b>Route</b>
      </th>
      <th><b>Arrival Time</b>
      </th>
      <th><b>Direction</b>
      </th>
    </tr>
  </table>
</div>
&#13;
&#13;
&#13;

0 个答案:

没有答案