使用jQuery的简单AJAX请求无法在IE上运行

时间:2021-02-28 01:25:43

This is my code, which sometimes works and sometimes doesn't.

这是我的代码,有时可以工作,有时却不工作。

var resolve_ajax_login=function(){
  $.ajaxSetup({cache:false });
  var loginvar=$("#inputlogin").attr("value");
  var senhavar=$("#inputsenha").attr("value");
  $.post("../model/php/login_ajax.php",
        {login:loginvar, senha:senhavar},
        function(responseText){
            if (responseText=="ok"){ 
                window.location="areatrab.php";

            }else{
                $("#inputlogin").attr("value","");
                $("#inputsenha").attr("value","");
                $("#divmensagem").html("<span style='color:red;font-size:70%;'>"+responseText+"</span>");

            }
        }
  );
  return false;
};

Ok. It's in portuguese but I think you get the general picture. Sometimes this works, no problem, but some other times (only in IE, no problem whatsoever in Firefox) it throws a javascript error in my jquery.js file (minified). The error description is as follows:

好。这是葡萄牙语,但我认为你得到了一般情况。有时这可行,没有问题,但有些时候(仅在IE中,在Firefox中没有任何问题)它会在我的jquery.js文件中抛出一个javascript错误(缩小)。错误描述如下:

Object doesn't support this property or method: jquerymin.js line 123 character 183..

对象不支持此属性或方法:jquerymin.js第123行字符183 ..

which amounts to...

相当于......

{return new A.XMLHttpRequest}

somewhere in the middle of the jquery.js file. It seems to be very IE-specific, as I had no such problems on Firefox. This guy apparently had the same problem as I did, but got no responses yet.

在jquery.js文件中间的某个地方。它似乎非常特定于IE,因为我在Firefox上没有这样的问题。这家伙显然遇到了和我一样的问题,但还没有回复。

Has anyone else seen this? Thanks in Advance

有没有人见过这个?提前致谢

P.S.: I run IE 8

P.S。:我运行IE 8

2 个解决方案

#1


1  

Have you tried using a full URL instead of ../model...? For example: http://www.mysite.com/model/login_ajax.php

您是否尝试使用完整的URL而不是../ model ...?例如:http://www.mysite.com/model/login_ajax.php

Also, maybe try modifying the 'xhr' property using jQuery's .ajax method... something like:

此外,也许尝试使用jQuery的.ajax方法修改'xhr'属性...类似于:


var loginvar = $("#inputlogin").val();
var senhavar = $("#inputsenha").val();
var ajax_obj = null;

var resolve_ajax_login = function() {
  if(ajax_obj !== null) {
    try {
      ajax_obj.abort();
    } catch(e) {
    }
  }

  ajax_obj = $.ajax({
    type: 'POST',
    cache: false,
    url: '../model/php/login_ajax.php',
    data: {login:loginvar, senha:senhavar},
    dataType: 'text',
    timeout: 7000,
    success: function(data)
    {
      if(response == 'ok') {
        alert("right on!");
      } else {
        alert("not ok");
        return;
      }
    },
    error: function(req, reqStatus, reqError)
    {
      alert("error");
      return;
    },
    'xhr': function() {
      if(ajax_obj !== null) {
        return ajax_obj;
      }

      if($.browser.msie && $.browser.version.substr(0,1) <= 7) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        return new XMLHttpRequest();
      }
    }
  });
}

#2


0  

It's something to do with the order in which you try all the different types of browsers in order to create the right kind of XMLHTTP REQUEST object.. I'll explain it in more detail in the following page:

这与您尝试所有不同类型的浏览器的顺序有关,以便创建正确类型的XMLHTTP REQUEST对象。我将在下面的页面中更详细地解释它:

AJAX inconsistency in IE 8?

IE 8中的AJAX不一致?

#1


1  

Have you tried using a full URL instead of ../model...? For example: http://www.mysite.com/model/login_ajax.php

您是否尝试使用完整的URL而不是../ model ...?例如:http://www.mysite.com/model/login_ajax.php

Also, maybe try modifying the 'xhr' property using jQuery's .ajax method... something like:

此外,也许尝试使用jQuery的.ajax方法修改'xhr'属性...类似于:


var loginvar = $("#inputlogin").val();
var senhavar = $("#inputsenha").val();
var ajax_obj = null;

var resolve_ajax_login = function() {
  if(ajax_obj !== null) {
    try {
      ajax_obj.abort();
    } catch(e) {
    }
  }

  ajax_obj = $.ajax({
    type: 'POST',
    cache: false,
    url: '../model/php/login_ajax.php',
    data: {login:loginvar, senha:senhavar},
    dataType: 'text',
    timeout: 7000,
    success: function(data)
    {
      if(response == 'ok') {
        alert("right on!");
      } else {
        alert("not ok");
        return;
      }
    },
    error: function(req, reqStatus, reqError)
    {
      alert("error");
      return;
    },
    'xhr': function() {
      if(ajax_obj !== null) {
        return ajax_obj;
      }

      if($.browser.msie && $.browser.version.substr(0,1) <= 7) {
        return new ActiveXObject("Microsoft.XMLHTTP");
      } else {
        return new XMLHttpRequest();
      }
    }
  });
}

#2


0  

It's something to do with the order in which you try all the different types of browsers in order to create the right kind of XMLHTTP REQUEST object.. I'll explain it in more detail in the following page:

这与您尝试所有不同类型的浏览器的顺序有关,以便创建正确类型的XMLHTTP REQUEST对象。我将在下面的页面中更详细地解释它:

AJAX inconsistency in IE 8?

IE 8中的AJAX不一致?