我是一個n00b試圖通過在線教程學習jquery .ajax
。jquery`.ajax`不工作
我有以下的在我的本地的「客戶」代碼:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.js"></script>
<script>
// wait for the DOM to be loaded
$(document).ready(function() {
$("#submit_btn").click(function(){
$.ajax({
type: "POST",
url: "response.php",
dataType: 'json',
data: { name: "John", location: "Boston" }
}).done(function(msg)
{
alert("Data Saved: " + msg);
});
});
});
</script>
</head>
<body>
<input type="submit" name="submit" class="button" id="submit_btn" value="Send" />
</body>
</html>
而且我有以下我的「服務器」端文件的代碼稱爲「response.php」:
<?php
if (isset($_POST))
{
$answer = $_POST;
echo json_encode($answer);
} else {
echo json_encode("no good");
}
?>
當我點擊'發送'按鈕時,我收到一條警告,提示「數據已保存:[對象對象]」。我試圖通過使用click事件來顯示通過`post'提交的數據。我希望警報顯示'姓名:'約翰',位置:'波士頓''。
檢查控制檯的任何錯誤,它可能會拋出一個解析錯誤 – 2013-04-22 03:31:35
我不是一個PHP的人,還是我對'json_encode疑慮(「不良」)'給一個JSON O/p – 2013-04-22 03:32:04
是你的要求給予'不好'作爲迴應 – 2013-04-22 03:32:52