-3

我正在使用XMLHttpRequest.

它在 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)。

4

2 回答 2

3

到目前为止,您显示的代码应该可以在 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从服务器收到 a,以确保没有服务器端错误,或者 URL 不正确。

于 2012-08-24T07:24:18.400 回答
1

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

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

于 2012-08-24T07:38:30.920 回答