在接收JSON数据时不清楚我哪里出错了

时间:2015-02-12 17:31:19

标签: php jquery ajax json

我花了近3个小时进行堆栈交换,谷歌试图拼凑各种信息。所以,这就是我对guess.php的看法。

PHP

header('Content-type: application/json');
get_rating($cleanMovie);
json_encode(array($id));

get_rating函数运行正常。

现在我想将一些数据发布到这个php表单中并从中获取字符串,但是因为当我尝试使用dataType:string时我得到了整个HTML代码。我认为使用json会更好,因此我做了上述修改而不是简单的“print $id;

AJAX

$.ajax({
type: "GET",
url: "guess.php",
data: { m: "Avenger"},
dataType: "json",
success: function(d) {
       alert(d);
    }
});

什么都没有打印出来。我希望它只打印字符串。而且,有可能摆脱所有的JSON并使用PHP页面上看到的字符串吗?

如何处理此输出?

我错过了很多概念。请澄清!

谢谢, 西

1 个答案:

答案 0 :(得分:3)

我打赌你应该echo回复:

- json_encode(array($id));
+ echo json_encode(array($id));

希望它有所帮助。