即使在ajax发布之后,也会调用失败函数,然后始终调用函数,但不调用done函数。它为什么会发生?
$('#askques').submit(function(event) {
event.preventDefault();
$.ajax({
type: 'POST',
url: 'post.php',
data: $(this).serialize(),
dataType: 'json',
})
.done(function(data) { alert( data );})
.fail(function() {alert( "error" ) ;})
.always(function(data)
{alert(data);
$(".sub").attr("disabled","1");
});
});
post.php中
<?php
//error_reporting(0);
//$data = json_decode(file_get_contents("php://input"));
$fname = $_POST["fname"]; //$data->name;
$lname = $_POST["lname"];//$data->fname;
$ques = $_POST["ques"];//$data->ques;
$ans = $_POST["ans"];//$data->ans;
$myfile = fopen("data.txt", "w") or die("Unable to open file!");
$txt = $fname." ".$lfname." ".$ques." ".$ans;
fwrite($myfile, $txt);
fclose($myfile);
//$data = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);
//echo json_encode($data);
echo "Done";
?>
答案 0 :(得分:1)
dataType: 'json',
指定服务器的返回类型必须是有效的JSON,但您的服务器不会发回JSON。从请求中删除dataType
,或将您的响应编码为JSON