Ajax 应用程序适用于某些浏览器,而不适用于其他浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1482177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 10:49:00  来源:igfitidea点击:

Ajax app works in some browers, not others

ajaxinternet-explorer-8

提问by Satish Ravipati

My ajax app works fine in Firefox, but not in IE8. Specifically, the ajax functionality doesn't work.

我的 ajax 应用程序在 Firefox 中运行良好,但在 IE8 中却无法运行。具体来说,ajax 功能不起作用。

Here's the code I'm using:

这是我正在使用的代码:

function createXMLHttpRequest()
{
  if (window.XMLHttpRequest)
  {
    // code for IE7+, Firefox, Chrome, Opera, Safari
    return new XMLHttpRequest();
  }

  if (window.ActiveXObject)
  {
    // code for IE6, IE5
    return new ActiveXObject("Microsoft.XMLHTTP");
  }

  return null;
}

This is the error:

这是错误:

Object doesn't support this property or method
ajax.js
Code:0
Line : 6
Char : 5

It works perfect in Firefox.

它在 Firefox 中完美运行。

What is the problem with my code ?

我的代码有什么问题?

采纳答案by meder omuraliev

Consider using a function such as this:

考虑使用这样的函数:

 function createXMLHttpRequest() {
  var xmlhttp = false;
  if (window.XMLHttpRequest) {
   xmlhttp = new XMLHttpRequest();
  } else if(window.ActiveXObject) {
   try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
   } catch (e) {
    try {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
     xmlhttp = false;
    }
   }
  }
  return xmlhttp;
 };

Which tests for the new XMLHttp plugin in ActiveX, or defaults to the old one.

其中测试 ActiveX 中的新 XMLHttp 插件,或默认为旧插件。

Update: Try this instead:

更新:试试这个:

function createXMLHttpRequest()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function getModIndex(val) {
    var divEle = "IndexDiv" + val;
    var request = createXMLHttpRequest();

    if ( !request ) { 
    alert( request )
    return false
    }

    var callback = function( oXML ) {
    document.getElementById( divEle ).innerHTML = oXML.responseText;
    }

    request.connect(
    '../ajax/ajax-GetIndex.php',
    'POST',
    'id=' + val,
    callback
    );
}

回答by Dan Beam

straight out of jQuery:

直接从jQuery:

return window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") :
                              new XMLHttpRequest();

回答by Colin

Can you try this code:

你能试试这个代码吗:

function createXMLHttpRequest() { 
  if (typeof XMLHttpRequest != "undefined") { 
    return new XMLHttpRequest();
  } else if (typeof ActiveXObject != "undefined") { 
    return new ActiveXObject("Microsoft.XMLHTTP"); 
  } else { 
    throw new Error("XMLHttpRequest not supported");
}

回答by blackstrype

I was having the same problem -- reloading the page seemed to eliminate the error, but visiting the page first time I got the same error as luvboy.

我遇到了同样的问题——重新加载页面似乎消除了错误,但是第一次访问页面时我遇到了与 luvboy 相同的错误。

I think the underlying problem has something to do with IE 8 having issues with a first-time load of the XMLHttpRequest function. I gave up searching for the real reason why, but it seems that others have had similar quirks: http://www.daniweb.com/forums/thread299941.html

我认为潜在的问题与 IE 8 在首次加载 XMLHttpRequest 函数时出现问题有关。我放弃了寻找真正原因,但似乎其他人也有类似的怪癖:http: //www.daniweb.com/forums/thread299941.html

Dan Beam's answer seems to work well enough and it's way less code -- it checks for the ActiveXObject first, which IE seems to be more content with.

Dan Beam 的答案似乎工作得很好,而且代码更少——它首先检查 ActiveXObject,IE 似乎更满意。

回答by cglezar

"I'm not sure if I figured out precisely what the problem was, but I did figure out a solution. I was going to recode it in jQuery and noticed that the code i was looking at checks for ActiveX before XMLHttpRequest, so I just flipped my code to also check for ActiveX first and now it works for both IE8 and FF.

“我不确定我是否准确地找出了问题所在,但我确实找到了解决方案。我打算在 jQuery 中重新编码它并注意到我正在查看的代码在 XMLHttpRequest 之前检查 ActiveX,所以我只是翻转我的代码以首先检查 ActiveX,现在它适用于 IE8 和 FF。

Seems like the problem is that IE8 isn't able to create a XMLHttpRequest after I reload the page (I have no idea why) so letting it create an ActiveXObject instead makes it work all the time." I Fount this answer here

似乎问题是在我重新加载页面后 IE8 无法创建 XMLHttpRequest(我不知道为什么)所以让它创建一个 ActiveXObject 而让它一直工作。”我在这里找到这个答案

It works All the time :D :D !!

它一直有效 :D :D !!

回答by Floyd Johnson

In IE8 on Vista at least, the user needs to turn off the "native XMLHTTP support" (sounds counterintuitive). To do this:

至少在 Vista 上的 IE8 中,用户需要关闭“原生 XMLHTTP 支持”(听起来违反直觉)。去做这个:

  1. Summon "Internet Options", then click the Advanced tab.
  2. Scroll down to the "Security" set of checkboxes (this could be a while), and uncheck "Enable native XMLHTTP support". You may need to also uncheck "Enable Integrated Windows Authentication" (also in the Security section) and restart IE8, per the discovery at http://community.xajax-project.org/topic/8540/access-is-denied-error-in-ie8/
  1. 调用“Internet 选项”,然后单击“高级”选项卡。
  2. 向下滚动到“安全”组复选框(这可能需要一段时间),然后取消选中“启用原生 XMLHTTP 支持”。根据http://community.xajax-project.org/topic/8540/access-is-denied-error的发现,您可能还需要取消选中“启用集成 Windows 身份验证”(也在安全部分)并重新启动 IE8 -in-ie8/

You might have your web application detect if this needs to be done by looking for a JavaScript exception whose "message" property is "Access is denied" (or something of that ilk) when an attempt is made to POST to an HTTPS URL via your AJAX object.

当尝试通过您的 HTTPS URL POST 到 HTTPS URL 时,您可能让您的 Web 应用程序通过查找“消息”属性为“访问被拒绝”(或类似的东西)的 JavaScript 异常来检测是否需要完成此操作。 AJAX 对象。

(With Prototype, you would add an onException key-value pair to the same place as onSuccess and onFailure. It would look something like: onException:function(irrelevant_object,exc){alert(exc.message);}

(使用 Prototype,您可以将 onException 键值对添加到与 onSuccess 和 onFailure 相同的位置。它看起来像: onException:function(irrelevant_object,exc){alert(exc.message);}

The function body will be much more involved than displaying the essence of the thrown exception.)

函数体将比显示抛出异常的本质要复杂得多。)

If the relevant exception is thrown, put up a box telling the user how to turn off the XMLHTTP item in his/her copy of IE8.

如果抛出相关异常,则在其 IE8 副本中放置一个框告诉用户如何关闭 XMLHTTP 项。

回答by NosyBonk

been having similar probs and have read loads of solutions that didnt work for me

一直有类似的问题,并阅读了大量对我不起作用的解决方案

When i finally figured it out I cursed microsoft even more than usual.

当我终于弄清楚时,我比平时更诅咒微软。

the following code will work in Mozilla as it decides if no value is set it uses value inbetween the option tags to send to SetContactForm, whereas IE8 sends a blank value

下面的代码将在 Mozilla 中工作,因为它决定是否没有设置任何值,它使用选项标签之间的值发送到 SetContactForm,而 IE8 发送一个空值

<select name="contact_type" id="contact_type" onChange="SetContactForm(this.value)">
<option>email</option>
<option>address</option>
</select>

to get it to working simply add a value

让它工作只需添加一个值

<select name="contact_type" id="contact_type" onChange="SetContactForm(this.value)">
<option value="email">email</option>
<option value="address">address</option>
</select>

I hope this saves some headscratching.

我希望这可以避免一些麻烦。

回答by vapcguy

This is code (location scrubbed) that I use for getting the contents of a text file... works in IE 8 and Chrome:

这是我用于获取文本文件内容的代码(位置已删除)...适用于 IE 8 和 Chrome:

var fileName = "http://www.example.com/myfile.txt";

var txtFile;

if (window.ActiveXObject)
{
    try {// code for IE8
        txtFile = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try { // code for IE6, IE5
            txtFile = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (e) {
            txtFile = false;
        }
    }
}
else if (window.XMLHttpRequest)
{
    // code for IE7, Firefox, Chrome, Opera, Safari
    txtFile = new XMLHttpRequest();
}

txtFile.open("GET",fileName,false);
txtFile.send();
var txtDoc=txtFile.responseText;
// alert(txtDoc); // this will give you the text contents

回答by Rajiv

You can try UpperLower case for Microsoft.XMLHTTP i.e. (Microsoft.XMLHttp) IE8 is exraordinarily Case Sensitive

你可以试试UpperLower case for Microsoft.XMLHTTP ie (Microsoft.XMLHttp) IE8 is exraordinarily Case Sensitive