2012-02-27 61 views
1

工作我使用XMLHttpRequest对象为我的AJAX调用已被罚款跨浏览器与我创建基于请求类型和参数等的XMLHttpRequest只能在IE

但我返回JSON回调处理程序工作我现在整合外部的RESTful API返回一个JSON字符串,由于某种原因它只能在IE(以IE 8测试)为我工作。使用fiddler 2我已经确定API正在返回正确的数据。

我得到的4 XMLHttpRequest.readyState但XMLHttpRequest.status仅适用于Chrome,Safari和FF返回0。我读了使用本地服务器(测试服务器)有时当你总是得到零状态,所以我绕过我的状态检查,但仍然得到了XMLHttpRequest.responseText一个空字符串。

function ajaxRequest(requestType,url) { 

    var xmlhttp; 

    if (window.XMLHttpRequest) { 
     xmlhttp = new XMLHttpRequest(); 
    } else { 
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
    } 

    xmlhttp.onreadystatechange = function() 
    { 

     if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
     { 
      switch (requestType) 
      { 
       case 5: 
        //Home postcode search 
        showAddresses("home", xmlhttp.responseText); 
        break;   
      } 
     } 
    } 
    xmlhttp.open("GET", url, true); 
    xmlhttp.send(); 
} 
+1

您的AJAX调用托管的脚本在哪里?您要请求的URL是什么? – robertc 2012-02-27 13:01:11

回答

0

我的问题是我使用的是外部API,而xmlHttpRequest只允许你在同一台服务器上进行调用。我动了我的数据呼叫到我的服务器代码,并得到了我的回调处理页面的响应,而不是去直出的API。

+0

这并不完全正确。如果您的浏览器和服务器都支持[CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing),那么XHR将允许您联系远程服务器。 – hippietrail 2012-02-28 13:11:21