【发布时间】:2012-05-06 01:57:34
【问题描述】:
我已经使用json_encode 很长时间了,到目前为止我还没有遇到任何问题。
现在我正在使用上传脚本,并尝试在文件上传后返回一些 JSON 数据。
我有以下代码:
print_r($result); // <-- This is an associative array
echo json_encode($result); // <-- this returns valid JSON
这给了我以下结果:
// print_r result
Array
(
[logo_url] => http://mysite.com/uploads/gallery/7f/3b/f65ab8165d_logo.jpeg
[img_id] => 54
[feedback] => Array
(
[message] => File uploaded
[success] => 1
)
)
// Echo result
{"logo_url":"http:\/\/mysite.com\/uploads\/gallery\/7f\/3b\/f65ab8165d_logo.jpeg","img_id":"54","feedback":{"message":"File uploaded","success":true}}
谁能告诉我为什么json_encode 加斜线?
更新
@Quentin 说 json_encode 和 .parseJSON 之间发生了一些事情,他是对的。
执行alert(data.toSource()); 会给我带来令人沮丧的结果:
({response:"{\"logo_url\":\"http:\\/\\/storelocator.com\\/wp-content\\/uploads\\/gallery\\/7f\\/3b\\/71b9520cfc91a90afbdbbfc9d2b2239b_logo.jpeg\",\"img_id\":\"62\",\"feedback\":{\"message\":\"File uploaded\",\"success\":true}}", status:200})
这不是有效的 JSON。它还添加了status:200,我不知道这是从哪里来的。
会不会是Plupload bind 对我返回的数据做了什么?
这是我的 js 脚本:
uploader.bind('FileUploaded', function(up, file, data) {
alert(data.toSource());
$('#' + file.id + " b").html("100%");
});
【问题讨论】:
-
您尝试使用搜索吗? stackoverflow.com/questions/5272311/…
-
是的,我已经搜索并看到了各种回复。问题是我没有做任何新的事情,我已经注意到在我的代码中已经做了一百次了。
-
“无效”json 看起来像是对 jsonp 请求的错误尝试,并且没有指定回调函数名称。
-
当我 json_encode 一个 json 发生这种情况!