【问题标题】:cant get json object to show using ajax/jquery无法使用 ajax/jquery 获取要显示的 json 对象
【发布时间】:2013-05-17 05:21:09
【问题描述】:

我对 JSON 了解不多,并且尽管我尝试了很多次,但始终无法使用 JSON 将某些内容返回到 ajax 并使用 jquery 显示它。
我想要做的是,使用 JSON 对象发送数据加载用户配置文件时到 ajax。

[更新] php 代码

<?php include(dirname(__FILE__). '/../script/config.php');
session_start();
$id = $_POST['u_search'];
$email = $_SESSION['Email'];

foreach($pdo->query("SELECT * FROM Users WHERE ID='$id'") as $row) {
    //$firstname = $row['FirstName'];
    //$lastname = $row['LastName'];
    $pic = $row['Pic'];
    $id = $row['ID'];
    $u_email = $row['Email'];
}
$firstname = "Jason";
    $lastname = "Born";
    $data = array("success"=> true,"inpt"=>"<p>Hello there! I am " . $firstname . " " . $lastname . "</p>");
    echo json_encode($data);
header("Content-Type: application/json");)
?>

<?php $pdo = null; ?>

更新的 Ajax

function op_prof(obj) {
    var value = obj.id;
    var dataString = "{'u_search':'"+value+"'}";
    $("#co_profile").show();
    $(".searchbox").val('');
    $("#usr_suggest").hide();

    $.ajax({
    type: "POST",
    url: '/script/profile.php',
    dataType: 'json',
    data: dataString,
    cache: false,
    success: function(data) {
        alert(console.log(data));
        alert(data);
        $("#co_profile").html(data.inpt).show();
        location.hash = 'profile' + 'id=' + dataString;
    }
  });
};

编辑:当我使用 dataType: 'json' 时,success 中没有任何内容运行,但是当我删除它时,它们运行..

编辑:当我使用datatype: 'json' 而不是dataType: 'json' 时,success 中的代码运行。我用 alert(console.log(data)); ,它说“未定义”

编辑:我正在使用//ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js

【问题讨论】:

  • 首先尝试console.log(data) in success 并在您的问题中打印日志。
  • 我可以根据您的喜好在我的示例中正确加载 json 数据。如果你愿意,我可以分享。但首先显示你的 console.log(data)。
  • success 中的任何内容都没有运行 .. 那是有线的 .. 我已成功输入 console.log(data);
  • 但是当我删除 dataType: 'json' 时,success 中的所有内容都会运行
  • 如果您运行 profile.php 文件,它会显示任何错误吗?因为我怀疑在您的第一个条件下使用'$id''$pic'

标签: jquery ajax json


【解决方案1】:

如果你输入 datatype:json 意味着你必须以 json 字符串格式发送 data:"" ,即使在服务器端,变量名也应该匹配,以便它可以读取你发送的值。

示例:

datatype:'json',
data : JSON.stringify({'u_search':'value'})

使用 JSON.js 文件将对象转换为字符串。

【讨论】:

    【解决方案2】:
    var dataString = "{'u_search':'"+value+"'}";
    
    dataType: 'json',
    data: dataString,
    

    编辑:

    <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    

    【讨论】:

      【解决方案3】:

      试试这个。

      $.post("/script/profile.php",{dataString:dataString},function(data){
                      alert(console.log(data));
          alert(data);
          $("#co_profile").html(data.inpt).show();
          location.hash = 'profile' + 'id=' + dataString;
                  }, "json");   // USing JSON here for the returned value from server...
      

      在您的 profile.php 文件中,您必须解码 json 格式。记得将解码后的 json 值收集到一个名为 dataString 的变量中

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-31
        • 1970-01-01
        • 2016-03-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多