2012-06-25 96 views
1

我只想使用ajax post請求將數據發送到遠程服務器。以下代碼在IE中正常工作,但不在Chrome中。爲什麼?什麼是「訪問控制 - 允許來源不允許」Origin http // localhost「問題?我怎樣才能使它在鉻也工作。請幫幫我。Ajax post請求在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 
    } 

回答

1

您不允許向其他服務器發出AJAX請求。您的腳本必須與您要發送AJAX請求的服務器在同一個域上運行,除非該服務器允許您使用上述訪問控制 - 允許來源來執行此操作。