Note that there are some explanatory texts on larger screens.

plurals
  1. POProblem to retrieve JSON after AJAX call
    primarykey
    data
    text
    <p>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</p> <p>Server side :</p> <pre><code>$array = array(); $array["count"] = count($accounts_list).""; for($i=0; $i&lt;count($accounts_list); $i++) { $account = $accounts_list[$i]; $array["marker"]["name"] = $account-&gt;name; $array["marker"]["lat"] = $account-&gt;map_latitude; $array["marker"]["lon"] = $account-&gt;map_longitude; } echo json_encode($array); </code></pre> <p>Client side :</p> <pre><code>$.ajax({ type: "POST", url: "index.php?module=cap_Maps&amp;action=AddMarkers", data: dataString, dataType: "json", success: function(data) { if (data == "error"){ $(".tr_legend").before("&lt;tr&gt;&lt;td colspan='2' id='error'&gt;&lt;span class='error_maps'&gt;Erreur lors du chargement des marqueurs&lt;/span&gt;&lt;td&gt;&lt;tr&gt;"); } else { alert(data); var obj = jQuery.parseJSON( data ) alert (obj.count); } } }); </code></pre> <p>JSON returns by server:</p> <pre><code>{"count":"371","marker":{"name":"WAMPFLER","lat":"49.02751610","lon":"2.10611230"}} </code></pre> <p>Function <code>alert(data)</code> returns my JSON, but if I try to parse it with <code>jQuery.parseJSON( data )</code>, It doesn't work and <code>alert(obj.count);</code> doesn't open.</p> <p><strong>EDIT</strong></p> <p>I have add error function:</p> <pre><code>error: function (XMLHttpRequest, textStatus, errorThrown) { alert("XMLHttpRequest="+XMLHttpRequest.responseText+"\ntextStatus="+textStatus+"\nerrorThrown="+errorThrown); }, </code></pre> <p>And error occured :</p> <pre><code>XMLHttpRequest={"count":"358"} textStatus=parsererror errorThrown=Invalid JSON: {"count":"358"} </code></pre> <p><strong>EDIT</strong></p> <p>If I had <code>contentType: "application/json",</code> 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 <code>500 Internal server error</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload