我想获取在 php 文件中回显的 Json 数组。问题是,当我在客户端中执行 $.getJSON 时,我收到错误:
SyntaxError: JSON.parse: unexpected character, return window.JSON.parse( data + "" );
代码: php:
echo json_encode($responseVar);
Json 数组在源中可见:{"resp":0}
js:
$.getJSON('../views/application.php', function(data) {
alert('alert1');
if(data) {
document.write(data.resp);
alert('alert2');
}
else {
alert('error');
}
})
.fail(function(j,t,e) {
console.error(e);
});
我没有收到任何警报,我只是收到了所描述的错误。
最佳答案
尝试将其放入函数中:
function getJsonArray()
{
var URL="/URL";
$.ajax(
{
type : "GET",
url : processURL,
cache : false,
dataType : "json",
success : function (Put the array name here)
{Do Something with the array }};
关于javascript - 当我想接收 json 数组时 $.getJSON() 返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21481151/