I'm trying to get the count of a variable using ajax but it keeps printing fail everytime
here is the php file that works perfectly when I test it
<?php
$conn= mysqli_connect("localhost","root","") or die ("could not connect to mysql");
mysqli_select_db($conn,"bdpfe") or die ("no database");
$rech=$_GET['q'];
$sql=mysqli_query($conn,"select count(id_com) as nbr from commentaire where id_pub like '".$rech."' ");
$response = array();
$nbrs = array();
$result=$sql;
while($row=mysqli_fetch_array($result))
{
$nbr=$row['nbr'];
$nbrs[] = array('nbr'=>$nbr);
}
$response['nbrs'] = $nbrs;
echo "mycallbackcom(".json_encode($response).")";
?>
and here is the ajax call using jsonp
(function getnbr() {
$.ajax({
type : 'GET',
url : 'http://127.0.0.1:800/test/count_com.php?callback=?&q='+$('#idpub').val() ,
jsonpCallback: 'mycallbackcom',
dataType: 'jsonp',
contentType: "application/json",
success: function (data) {
alert("succes");
},
error: function () {
alert("fail");
}
});
})(jQuery);
and the callback function is empty for now:
function mycallbackcom()
{}
it keeps printing fail every time.
Try only returning the JSON. You have specified this as your expected return dataType.
Then handle the response: