javascript - 当我想接收 json 数组时 $.getJSON() 返回错误

标签 javascript php jquery json

我想获取在 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/

相关文章:

javascript - 如何处理 react 中的异步功能?

javascript - 关于JavaScript的TypeError异常的一个问题

javascript - 在 Angular 2 子模块中强制服务实例化(AngularJS 运行 block 的替代方案)

php - 从mysql获取结果并将每个结果应用到变量并最终输出

php - 流明访问 Controller 外部的请求对象

javascript - 如何使用 RequireJS 销毁加载的 Javascript 文件

php - 如何在 PHP 中创建表格的纯文本表示形式?

javascript - 下载属性在 Firefox 中不起作用

jquery - 单击 anchor 链接时淡入/淡出音乐

javascript - jquery on 方法上的多个函数