Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法将PHP数组传递给jquery:JSON数据后出现意外的非空白字符_Php_Jquery_Json - Fatal编程技术网

无法将PHP数组传递给jquery:JSON数据后出现意外的非空白字符

无法将PHP数组传递给jquery:JSON数据后出现意外的非空白字符,php,jquery,json,Php,Jquery,Json,以下是我在PHP中的内容: $arrayresults = array(); while($popularbrushesrow = mysql_fetch_array($popularBrushes)){ $arrayresults[] = '<a href="brushdescription.php?id='.$popularbrushesrow['bd_brushid'].'"><img class="slideImg" alt="'.$popularbrushesro

以下是我在PHP中的内容:

$arrayresults = array();
while($popularbrushesrow = mysql_fetch_array($popularBrushes)){

$arrayresults[]  = '<a href="brushdescription.php?id='.$popularbrushesrow['bd_brushid'].'"><img class="slideImg" alt="'.$popularbrushesrow['bd_brushname'].'" title="'. $popularbrushesrow['bd_brushname'].'" src="'.$popularbrushesrow['bd_imagefilepath'].'"  /></a>';

}

echo json_encode($arrayresults);

谁能帮我一下吗。在JSON中形成数组的正确方法是什么?

问题可能是这一行:

var arrayFromPHP = JSON.parse(result);
因为您已经在
ajax
选项中指定了
dataType:'json'
,jQuery已经为您完成了解析。因此,第二次执行该操作首先在数组上执行
toString
,这会执行
join
,这会导致无效的JSON

直接使用
结果

例如,假设您有以下JSON:

[
    "<a href=\"http://stackoverflow.com\">Stack Overflow</a>",
    "<a href=\"http://google.com\">Google</a>"
]

…这当然不是有效的JSON。

我会简化您的jquery…如下所示

$.getJSON("getDataForSlide.php", { limit: limit, required: required}, function(json) {
console.log(json);
});
我喜欢使用

jQuery.parseJSON(response);

别忘了使用die();或退出();在php端,在您回显结果之后,因为这是一个ajax调用。此信息可在此处找到:

您是否确实返回了任何内容?该数据实际上是附加到GET的吗?如果您指定了数据类型,jQuery将已经为您解析JSON。执行
console.log(result)
并查看结果。可能您在json_encode调用后有一个关闭脚本或输出非json数据的东西,导致了错误。e、 一个html页脚或什么的。当你控制台日志(结果)时,你会得到什么;谢谢大家。。明白了我没做标记…:)但这可能是因为他使用json作为数据类型,所以解析已经为他处理了
$.getJSON("getDataForSlide.php", { limit: limit, required: required}, function(json) {
console.log(json);
});
jQuery.parseJSON(response);