我们如何在Firefox中调用AJAX

How can we call AJAX in Firefox

本文关键字:调用 AJAX Firefox 我们      更新时间:2023-09-26

我正在使用XMLHttpRequest进行AJAX调用。

它在IE7中运行良好,但当我在Firefox中尝试同样的操作时,我无法通过response.write 将其恢复

我正在使用以下功能:

<script type="text/javascript">
        function ddSelect_Change() {
          var xmlhttp;
            if (window.XMLHttpRequest) { // Mozilla, Safari, ...       
                xmlhttp = new XMLHttpRequest();
            }
            else if (window.ActiveXObject) { // IE       
                try {
                    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
                }
                catch (e) 
                {
                    try 
                    {
                        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    catch (e) 
                    {
                    }
                }
            }   
 xmlhttp.onreadystatechange = function () {
                //alert(xmlhttp.responseText);
                if (xmlhttp.readyState === 4 && xmlhttp.status === 200) {
}
}
 var url = "http://" + location.hostname + "Locationurl?Method=methodname";
xmlhttp.open("POST", url);
               xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xmlhttp.send(); 
}

添加

我有两个独立的web应用程序,一个是tridion web应用程序另一个是自定义web应用程序。我正在进行从tridion web应用程序到自定义web应用程序的交互。这两个url都有不同的域。状态我在firefox中得到0,而对于readystate,我在警报中没有得到(3)。

到目前为止显示的代码应该可以在Firefox中使用。Firefox支持XHR。

这可能会有所帮助:https://developer.mozilla.org/en-US/docs/AJAX/Getting_Started

更新:

onreadystatechange在AJAX调用过程中被激发了好几次,所以您可能想将回调扩展到以下内容:

xmlhttp.onreadystatechange = function () {
    if (xmlhttp.readyState === 4) {
      if (xmlhttp.status === 200) {
        alert(xmlhttp.responseText);
      } else {
        alert('There was a problem with the request.');
      }
    }
  }

xmlhttp.readyState === 4验证请求是否已完成,这样您就不会试图在收到响应之前向其发出警报。xmlhttp.status === 200验证您是否从服务器收到了200 OK,以确保没有服务器端错误或URL不正确。

您是否考虑过使用像jQuery这样的库?它已经为您解决了这些问题。

如果您正在开发SDL Tridion GUI扩展,请查看PowerTools项目中的大量示例。(http://code.google.com/p/tridion-2011-power-tools/)