我在這個問題上已經花了幾個小時,並且找不到解決方案。 我網頁上的下列服務(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遠程調用它時 - 它不起作用。它也適用於我手動發送表單。請幫忙,因爲我沒有任何新想法出了什麼問題。
你在'var action'中提到的完全相同的域和URL嗎? – random 2012-08-12 23:49:37
這可以幫助你http://stackoverflow.com/questions/3897641/can-ajax-request-data-from-a-remote-server – svakak 2012-08-13 15:58:24
你是什麼意思 - 「不工作」?使用FireBug等特殊擴展名進行調試。你需要一個服務器響應(正文,HTTP狀態等)。 – biakaveron 2012-08-14 05:11:09