php - json_encode return undefined - Stack Overflow

My script returns undefined value from my json_encode phpindex.php <?php$returnThis['user'

My script returns undefined value from my json_encode php

index.php

<?php
    $returnThis['user'] = "Robin098";
    $returnThis['id'] = "08465";

    echo json_encode($returnThis);
?>

sample.html

<head>
    <script>
        function clickHere(){
            $.get("index.php", function(data) {
            alert(data.user);
            });
        }

    </script>
</head>
       <body>
       <input type="button" onclick = "clickHere();" value="ClickHere!"/> 
       </body>

How can I fix this?

My script returns undefined value from my json_encode php

index.php

<?php
    $returnThis['user'] = "Robin098";
    $returnThis['id'] = "08465";

    echo json_encode($returnThis);
?>

sample.html

<head>
    <script>
        function clickHere(){
            $.get("index.php", function(data) {
            alert(data.user);
            });
        }

    </script>
</head>
       <body>
       <input type="button" onclick = "clickHere();" value="ClickHere!"/> 
       </body>

How can I fix this?

Share Improve this question edited Dec 9, 2011 at 18:17 Robin Carlo Catacutan asked Dec 9, 2011 at 18:06 Robin Carlo CatacutanRobin Carlo Catacutan 13.7k12 gold badges54 silver badges86 bronze badges 1
  • Where does the $aReturn variable e from in your PHP script? You go from $returnThis to $aReturn with no explanation. – Jasper Commented Dec 9, 2011 at 18:09
Add a ment  | 

2 Answers 2

Reset to default 3

Use the jQuery.getJSON method instead of .get, if you want your JSON to be parsed. Also, make sure that the jQuery library is correctly loaded.

    function clickHere(){
        $.getJSON("index.php", function(data) {
            alert(data.user);
        });
    }

Currently, you're using $.get(url, function(data){...}). In this context, data is a string containing the response from the server:

{"user":"Robin098","id":"80465"}

Using alert(data) inside the function will show this string.

It looks like you're setting up $returnThis, but then returning $aReturn. Don't you want:

$returnThis['user'] = "Robin098";
$returnThis['id'] = "08465";

echo json_encode($returnThis); 

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745405930a4626314.html

相关推荐

  • php - json_encode return undefined - Stack Overflow

    My script returns undefined value from my json_encode phpindex.php <?php$returnThis['user'

    13小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信