简体   繁体   English

json_encode数组出现问题

[英]Issues with json_encode array

I am having issues with my JSON_encode and jQuery, I am getting it to post the full array with the following code, but I can't get it to echo just the one array I need. 我的JSON_encode和jQuery有问题,我用下面的代码发布完整的数组,但是我无法让它仅回显我需要的一个数组。

$.get("/indexget.php", { page: "goals" },function(data) {
$('#bodyContentStuff').text(data);

Full jQuery code 完整的jQuery代码

$("#navLinkGoals").click(function () {
    $("#bodyTitleBar").text("Goals");
    $.get("/indexget.php", { page: "goals" },function(data) {
        $('#bodyContentStuff').text(data);
    }
);

Full PHP code 完整的PHP代码

if($_REQUEST['page'] = 'goals'){
    $result = mysql_query("SELECT * FROM content WHERE page='indexGoals'") 
    or die(mysql_error()); 
    $row = mysql_fetch_array( $result );
    $message = $row['message'];
}
echo json_encode(array("content"=>"$message"));

I am sort of a noob with programming, but the site is http://www.gronge.com if you'd like to see what it is doing, just click the goals link. 我有点编程方面的菜鸟,但是如果您想了解它的作用,则站点为http://www.gronge.com ,只需单击目标链接。

Edit: Oh, I have also tried 编辑:哦,我也尝试过

$('#bodyContentStuff').text(data['content']);

But it didn't work. 但这没有用。

have you tried putting ", 'json'" like so: 您是否尝试过将“,'json'”这样放置:

$.get("/indexget.php", { page: "goals" },function(data) {
    $('#bodyContentStuff').text(data.content);
}, 'json')

without that jquery makes an intelligent guess as to what the format is... 没有那个jQuery,就会聪明地猜测格式是什么...

and as mentioned it should be data.content 如前所述,应该是data.content

and as mentioned use console.log(data) to see what data really is (maybe a string while it should be a json object?) 并如前所述,使用console.log(data)来查看真正的数据(也许是字符串,而它应该是json对象?)

If that was a verbatim paste there is a missing closing parenthesis on the call to $.get 如果是逐字粘贴,则对$ .get的调用中缺少结束括号

$("#navLinkGoals").click(function () {
    $("#bodyTitleBar").text("Goals");
    $.get(
        "/indexget.php",
        {
            page: "goals"
        },
        function(data) {
            $('#bodyContentStuff').text(data);
        }
    ); // <== Right here
);

(Code reformatted to highlight difference) (重新格式化代码以突出显示差异)

Just noticed a potential issue with the PHP: 刚刚注意到PHP的潜在问题:

if($_REQUEST['page'] = 'goals'){ //<== are you assigning or comparing?
    $result = mysql_query("SELECT * FROM content WHERE page='indexGoals'") 
    or die(mysql_error()); 
    $row = mysql_fetch_array( $result );
    $message = $row['message'];
}
echo json_encode(array("content"=>"$message"));

instead of get use $.post 而不是使用$ .post

 $("#navLinkGoals").click(function () {
   $("#bodyTitleBar").text("Goals");
    $.post("/indexget.php", { page: "goals" },function(data) {
    $('#bodyContentStuff').text(data);
}
);

Have you logged the data variable? 您是否记录了data变量? What does it log? 它记录什么? Also: mysql_* is deprecated, steer clear and start using PDO. 另外: mysql_*已过时,请清除并开始使用PDO。 But I think you meant to assign $row[0]['message'] to the $message variable. 但是我认为您打算将$row[0]['message']分配给$message变量。 If memory serves me well, mysql_* always returns either an array of assoc arrays, or an array of arrays. 如果内存对我mysql_* ,则mysql_*总是返回assoc数组的数组或数组的数组。

While we're on the subject: JavaScript doesn't have assoc arrays, only objects, so rather then accessing the data like data['content'] , use data.content . 当我们讨论这个主题时:JavaScript没有assoc数组,只有对象,因此,使用data.content而不是访问诸如data['content']类的data['content'] The bracket notation is there for convenience, so you can use the value of a variable as property name (without eval), which is EVIL , of course. 括号表示法是为了方便起见,因此您可以将变量的值用作属性名(不带eval),当然是EVIL

Also on your php: 也在您的php上:

$row = mysql_fetch_assoc($result);

And not mysql_fetch_array, if you want to use the $row['message'] notation. 如果要使用$row['message']表示法,则不是mysql_fetch_array。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM