php – json_encode返回undefined

我的脚本从我的json_encode PHP返回未定义的值

的index.PHP

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

    echo json_encode($returnThis);
?>

sample.html

PHP",function(data) {
            alert(data.user);
            });
        }

    

我怎样才能解决这个问题?

最佳答案
如果要分析JSON,请使用jQuery.getJSON方法而不是.get.此外,请确保正确加载jQuery库.

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

目前,您正在使用$.get(url,function(data){…}).在此上下文中,data是一个包含服务器响应的字符串:

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

函数内使用alert(data)将显示此字符串.

相关文章

页面搜索关键词突出 // 页面搜索关键词突出 $(function () {...
jQuery实时显示日期、时间 html: <span id=&quot...
jQuery 添加水印 <script src="../../../.....
中文:Sys.WebForms.PageRequestManagerParserErrorExceptio...
1. 用Response.Write方法 代码如下: Response.Write(&q...
Jquery实现按钮点击遮罩加载,处理完后恢复 思路: 1.点击按...