JSON.parse() throws unexpected token error. Don't know why?
本问题已经有最佳答案,请猛点这里访问。
PHP
1 2 3 4 5 6 | <?php header("Content-type: application/json") ; echo json_encode(array("reply" =>"SOME_TEXT")) ; ?> |
与此相呼应->
1 | {"reply":"SOME_TEXT"} |
JavaScript
1 2 3 | $.get("script.php" , function(data) { var reply = JSON.parse(data) ; }) ; |
JS抛出
如果头设置为
1 2 3 | $.get("script.php" , function(data) { console.log(data.reply); }) ; |
正如@charlietft评论的那样,
using $.getJSON() instead of $.get() will also assure it is parsed