嗨,这是我的代码..
<html>
<head>
<script src="http://code.jquery.com/jquery-1.8.0.min.js">
</script>
</head>
<body>
<form id="foo">
<label for="bar">A bar</label>
<input id="bar" name="bar" type="text" value="" />
<input type="submit" value="Send" />
</form>
<div id ="onsuccess">
</div>
<script>
// variable to hold request
var request;
// bind to the submit event of our form
$("#foo").submit(function(event){
var $form = $(this);
var serializedData = $form.serialize();
request = $.ajax({
url: "a.txt",
type: "post",
dataType: "text",
data: serializedData,
success: function(){
console.log("yes, its successful");},
error : function(){console.log("got an error");}
});
});
</script>
</body>
</html>
我正在尝试访问位于同一目录中的 a.txt。但是成功和错误功能永远不会调用,这是无法理解的。Firebug 网络状态显示没有调用。奇怪
但是,如果我使用
request.done(function (response, textStatus, jqXHR){
// log a message to the console
console.log("Hooray, it worked!");
//$("#success").html(response);
});
它确实有效。