2012-08-12 136 views
0

我在這個問題上已經花了幾個小時,並且找不到解決方案。 我網頁上的下列服務(Kohana的3.2):Kohana 3.2 ajax jquery POST請求工作時,Ajax不。

public function action_test() { 
    $data = file_get_contents('php://input'); 
    $json = json_decode($data); 

    $t = array(); 
    $t[Utils::$KEY_LOGIN]  = isset($json->login) ? $json->login : arr::get($_POST, Utils::$KEY_LOGIN, null); 
    $t[Utils::$KEY_PASSWORD] = isset($json->password) ? $json->password : arr::get($_POST, Utils::$KEY_PASSWORD, null); 

    $returned = array("success" => true); 
    $returned = array_merge($returned, array("login" => $t[Utils::$KEY_LOGIN])); 

    $this->auto_render = FALSE; 
    ob_clean(); 
    $this->request->headers['Content-Type'] = "application/json"; 
    $this->response->body(json_encode($returned)); 
} 

而且形式詢問服務:

<html> 
<head> 
    <meta charset="utf-8" /> 
    <script type="text/javascript" src="//media/js/jquery-1.6.2.min.js"> </script> 
    <script type="text/javascript" src="//media/js/jquery-ui.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function() { 
      $("#login_sb").click(function() { 
       //var action = 'http://www.aaa.pl/clientservices/test'; 
       var action = "http://localhost/aaa/clientservices/test"; 
       $.ajax({ 
        url : action, 
        type : "post", 
        dataType : "json", 
        cache : "false", 
        timeout: 8000, 
        data : { 
         'login': '[email protected]' 
        }, 
        success : function(data) { 
         alert(data); 
         if(data.success) { 
          alert(data.login); 
         } 
        } 
       }); 
// 
       return false; 
      }); 
     }); 

    </script> 
</head> 
<body> 
    <form id="login_form" method="post"> 
     <input type="submit" value="login" id="login_sb"/> 
    </form> 

</body> 

當我致電當地服務的一切工作正常。但是當我試圖通過Ajax遠程調用它時 - 它不起作用。它也適用於我手動發送表單。請幫忙,因爲我沒有任何新想法出了什麼問題。

+0

你在'var action'中提到的完全相同的域和URL嗎? – random 2012-08-12 23:49:37

+0

這可以幫助你http://stackoverflow.com/questions/3897641/can-ajax-request-data-from-a-remote-server – svakak 2012-08-13 15:58:24

+2

你是什麼意思 - 「不工作」?使用FireBug等特殊擴展名進行調試。你需要一個服務器響應(正文,HTTP狀態等)。 – biakaveron 2012-08-14 05:11:09

回答

0

你可能試圖從另一個域調用AJAX請求嗎?

如果你正在嘗試致電該網址Ajax請求:從你的本地設置「http://www.aaa.pl/clientservices/test」

你會得到錯誤,Ajax請求必須是由相同的域名製作而成。

文件一個本地主機無法向www.aaa.pl發出ajax請求,因爲它是javascript中的一個不同的域和安全問題。

+0

完全和你寫的一樣。我們正在使用Phonegap編寫我們的應用程序。我們不知道域名必須相同。沒有一切正常工作 - 非常感謝您的幫助! – Konrad 2012-11-04 12:25:37