Reputation: 65
fail function is being called even after the ajax post has been made followed by always function being called but done function is not called. Why it's happening ?
$('#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";
?>
Upvotes: 0
Views: 49
Reputation: 28539
The dataType: 'json',
designates that the return type from server must be a valid JSON, yet your server is not sending JSON back. Either remove the dataType
from request, or encode your response as JSON
Upvotes: 1