My Ajax script keeps returning a result of unsuccessful. The console doesn't appear to show any errors. It just keeps throwing the alert window saying "error" instead of showing the result.
Ajax
$.ajax
({
url: 'query.php',
type: 'POST',
dataType:'json',
data: {Category: 'test'},
success: function(result)
{
if(result)
{
console.log(result);
alert(result);
}
},
error: function()
{
alert("error");
}
}); // end ajax call
php
$cat = $_POST['Category'];
echo $cat;
response a json
from php
example: echo "{}";
remove dataType: 'json'
please check your query.php
page. and make sure this page return some response either data or error massage like statusText="Error"
.
Do ajax function like below
$.ajax({ url: 'query.php',
type: 'POST',
dataType:'json',
data: {Category: 'test'},
success: function(result)
{
if(result.success=="TRUE")
{
console.log(result);
alert(result.cat);
}
},
error: function()
{
alert("error");
}
});
In query.php
$cat=$_POST['category'];
if(!empty($cat)){
$data['success']="TRUE";
$data['message'] = "Category Found.";
$data['cat']='$cat';
}
else{
$data['success'] = "False";
$data['message'] = "Category not found.";
}
die(json_encode($data));