代码之家  ›  专栏  ›  技术社区  ›  David

AJAX没有通过IE7/IE8中的POST变量

  •  4
  • David  · 技术社区  · 14 年前

    请注意:“使用jquery”或“使用(插入众所周知的框架)”的回答没有帮助。像jquery这样的框架包含了大量额外的代码,这对我现在所做的工作来说根本不是必需的但是,你可以包括一个从谷歌',是的,这可能是这样的,但我宁愿保留我自己的代码。考虑到这一点,让我们继续讨论这个问题。。。。

    我有一个ajax调用,它不会在IE7/IE8上传递POST vars,但只在一些奇怪的情况下传递。它似乎是非常随机的,而且大多数时候它确实起作用。

    以下是ajax函数:

    function GetXmlHttpObject(handler){ 
      var objxml = null;
     if(handler==null) {
      handler = function() {}
     }
    
       var ProgID = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0","Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];              
    
         try {  
             objxml = new XMLHttpRequest();  
         }  
         catch(e) {  
             for (var i = 0; i < ProgID.length; i++){  
                 try {  
                     objxml = new ActiveXObject(ProgID[i]);  
                 }  
                 catch(e) {  
                     continue;  
                }  
             }  
         } 
     objxml.onreadystatechange=handler;
    
         return objxml;  
    }
    

    调用Ajax函数的函数如下所示:

    function sample_ajax(object_type,object_id) {
     var d = new Date();
     var time = d.getTime();
     var url= MYSITEURL + "my_ajax_script.php?timestamp="+time;
    
     params = "object_type="+object_type+"&object_id="+object_id;
     xmlHttp_comment_notifyreset = GetXmlHttpObject(sample_ajax_helper);//fails on safari 1
     xmlHttp_comment_notifyreset.open("POST", url , true);
    
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "no-cache");
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "no-store");
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "must-revalidate");
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "post-check=0");
     xmlHttp_comment_notifyreset.setRequestHeader("Cache-Control", "pre-check=0");
     xmlHttp_comment_notifyreset.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
    
    
     xmlHttp_comment_notifyreset.setRequestHeader("Content-Type", "application/x-www-form-URLencoded");
     xmlHttp_comment_notifyreset.setRequestHeader("Content-Length", params.length);
     xmlHttp_comment_notifyreset.setRequestHeader("Connection", "close");
    
     xmlHttp_comment_notifyreset.send(params);
    }
    

    6 回复  |  直到 14 年前
        1
  •  2
  •   Annie    14 年前

    如果参数只是有时无法发送,问题可能出在这一行:

    params = "object_type="+object_type+"&object_id="+object_id;
    

    参数没有被转义。试试这个:

    params = "object_type=" +
             encodeURIComponent(object_type) +
             "&object_id=" +
             encodeURIComponent(object_id);
    
        2
  •  0
  •   Luca Matteis    14 年前
        3
  •  0
  •   Matt Mitchell    14 年前

    帮助不大,但这篇文章似乎很详细: https://developer.mozilla.org/en/AJAX:Getting_Started

        4
  •  0
  •   Alex    14 年前

    我在jquery中遇到了类似的问题,post参数无法通过。这更多地与asp.net读取post参数的方式有关。 http://www.bytechaser.com/en/functions/2jxhy5gg7w/read-ajax-post-parameters-in-asp-net.aspx . 希望有帮助

        5
  •  0
  •   mvds    14 年前

    我不记得见过像“application/x-www-form-URLencoded”这样的大写字符的内容类型,我也不知道它是否符合规范I 但是,可以看到,由于请求中缺少Content-Type头,PHP忽略了POST(因为一个代理人决定把它排除在外)

    PHP忽略任何它无法识别的内容类型。这将解决任何问题的可能性很小,但你可能会给它一个改变的机会 URL url