我见过一些类似的问题,但我没有看到具体说明这一点。我创建了一个非常简单的示例,我觉得它应该可以工作,但事实并非如此。关键是要看到一些简单的事情,以便其他类似的事情很清楚。如何让我的jQuery和PHP与ajax一起工作?
我觉得这是非常“基本”的,很难简单得多;因此,人们应该能够得到它的后面,知道这是最终的noobie敲门砖:
的HTML和JS:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<script type="javascript"></script>
<script type="text/javascript" src="/javascript/jquery-1.8.2.js">
$(document).ready(function(){
$("submit").click(function(){
var req = $.ajax({
type: 'POST',
url: 'form.php',
data: {
message: $('#message').val(),
author: $('#author').val()
},
timeout: 20000,
beforeSend: function(msg) {
$("#sent").html(msg);
}
});
req.fail(function(xhr, ajaxOptions, thrownError) {
alert("AJAX Failed");
});
req.done(function(res) {
$("#received").html(res);
});
});
});
</script>
</head>
<body>
<div id="sent"></div>
<div id="form">
<form>
Your message: <input type="text" name="message" value="Hi!" /><br />
Your name: <input type="text" name="author" value="Michael" /><br />
<input type="submit" name="submit" value="Submit me!" />
</form>
</div>
<div id="received"></div>
</body>
</html>
而且PHP:
<?php
echo "The file is located at ".$_POST["message"].".<br>";
echo "The file is named ".$_POST["author"].".";
什么是或不是它在做什么?从它的外观来看,我怀疑它实际上是提交表单并重新加载页面,而不是停止使用ajax请求。 –
@MichaelBerkowski我正在更新它的一些内容...截至目前,点击提交不会做任何事情。我试图看到发生的事情是点击提交输出数据到'#发送',然后到'#收到' –