gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:33:54 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com