1

我只想使用 ajax 发布请求将数据发送到远程服务器。以下代码在 IE 中运行良好,但在 chrome 中运行良好。为什么 ?什么是“Access-Control-Allow-Origin 不允许 Origin http//localhost”问题?我怎样才能让它在 chrome 中也能工作。请帮我。

 var http = new ajaxRequest();
        var url = "http://abcd.abc.com/login";
        var params = "username=name&password=pass&id=12345";
        http.open("POST", url, true);

        //Send the proper header information along with the request
        http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http.setRequestHeader("Content-length", params.length);
        http.setRequestHeader("Connection", "close");

        http.onreadystatechange = function() {
            //Call a function when the state changes.
            if(http.readyState == 4 && http.status == 200) {
                window.alert(http.responseText);
            }
        }
        http.send(params);


  function ajaxRequest(){
     var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
     if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
      for (var i=0; i<activexmodes.length; i++){
       try{
        return new ActiveXObject(activexmodes[i])
       }
       catch(e){
        //suppress error
       }
      }
     }
     else if (window.XMLHttpRequest) // if Mozilla, Safari etc
      return new XMLHttpRequest()
     else
      return false
    }
4

1 回答 1

1

您不能向其他服务器发出 AJAX 请求。您的脚本必须在与您发出 AJAX 请求的服务器相同的域上运行,除非该服务器允许您使用上述 Access-Control-Allow-Origin 来执行此操作。

于 2012-10-12T21:40:07.817 回答