跨域ajax POST ie7与jquery

时间:2012-10-17 16:33:52

标签: javascript jquery ajax internet-explorer-7 xdomainrequest

这个脚本一直遇到问题,我已经设法让它在ie8中工作,对Chrome很好。

initilize: function(){
 $('#my_form').submit(function(){
  if ($.browser.msie && window.XDomainRequest) {     
   var data = $('#my_form').serialize();
   xdr=new XDomainRequest();
   function after_xhr_load()
   {
    response = $.parseJSON(xdr.responseText);
    if(response.number =="incorrect format"){
     $('#errors').html('error');
    }
    else
    {
     $('#errors').html('worked');
    }
   }
   xdr.onload = after_xhr_load;
   xdr.open("POST",$('#my_form').attr('action')+".json");
   xdr.send(data);

  } else {
   $.ajax({
    type: "POST",
    url: $('#my_form').attr('action')+".json",
    data: $('#my_form').serialize(),
    dataType: "json",
    complete: function(data) {
     if(data.statusText =="OK"){
      $('#errors').html('error');
     }
     if(data.statusText =="Created"){
      response = $.parseJSON(data.responseText);
      $('#errors').html('Here is your code:' +response.code);
     }
    }
  });
 }
 return false;
});
}

我知道ie7没有XDomainRequest()对象。我怎样才能在ie7中复制这个。

先谢谢

1 个答案:

答案 0 :(得分:4)

您不会在IE7中使用该代码,因为旧浏览器不支持跨域调用。您需要更改后端以执行JSONP调用,或者需要使用服务器端代理。