PHP script json_encode mysql request cannot pass through to getJSON()

偶尔善良 提交于 2019-12-06 16:00:12

You need to add a header to output it as json in PHP script: json_encoded_array.php

 header("Content-type: application/json");

By default PHP will return text/html which is not a valid JSON for $.getJSON()

The MIME media type for JSON text is application/json. The default encoding is UTF-8. (Source: RFC 4627).

$.each takes a javascript array or object to loop through not a json string first you need to parse your json string using

jQuery.parseJSON()

so your code will look like this

data = jQuery.parseJSON(data);

    $.each(data, function (key, val) {
        $('ul').append('<li id="' + key + '">' + val.active  + '</li>');
    });

Found that default License Header from Netbeans (which is automatically generated) prevented Javascript from recognizing the JSON structure.

After removing the default License Header by modifying the default License Header to blank. The output from PHP script only contains the JSON structure. The browser display correctly.

Please compare the two debug output below (Cannot post images) debugFalse4.jpg: http://www.arthurmak.hk/debugFalse4.jpg debugFalseWrking.jpg: http://www.arthurmak.hk/debugFalseWorking.jpg

Thanks a lot to Saquieb showing how to obtain the debug information!!!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!