I have made an AJAX function with jQuery. The function works properly, and server return a JSON, but I can't use it in my JS script
Server side :
$array = array();
$array["count"] = count($accounts_list)."";
for($i=0; $i<count($accounts_list); $i++)
{
$account = $accounts_list[$i];
$array["marker"]["name"] = $account->name;
$array["marker"]["lat"] = $account->map_latitude;
$array["marker"]["lon"] = $account->map_longitude;
}
echo json_encode($array);
Client side :
$.ajax({
type: "POST",
url: "index.php?module=cap_Maps&action=AddMarkers",
data: dataString,
dataType: "json",
success: function(data) {
if (data == "error"){
$(".tr_legend").before("<tr><td colspan='2' id='error'><span class='error_maps'>Erreur lors du chargement des marqueurs</span><td><tr>");
}
else {
alert(data);
var obj = jQuery.parseJSON( data )
alert (obj.count);
}
}
});
JSON returns by server:
{"count":"371","marker":{"name":"WAMPFLER","lat":"49.02751610","lon":"2.10611230"}}
Function alert(data) returns my JSON, but if I try to parse it with jQuery.parseJSON( data ), It doesn't work and alert(obj.count); doesn't open.
EDIT
I have add error function:
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown);
},
And error occured :
XMLHttpRequest={"count":"358"}
textStatus=parsererror
errorThrown=Invalid JSON: {"count":"358"}
EDIT
If I had contentType: "application/json", in my AJAX, I can return a static string which is considered as json, but if I try to execute other php code on my server, AJAX returns an 500 Internal server error
{"count":"358"}is completely valid.