Javascript 我能';t将json对象从我的php传递到我的jquery

Javascript 我能';t将json对象从我的php传递到我的jquery,javascript,php,jquery,json,Javascript,Php,Jquery,Json,我试图将一个json对象从test.php传递到index.html中的jquery,但我做不到。 下面是我的test.php脚本: <?php if(isset($_POST["view"])) { $output .= ' <li><a href="#" class="text-bold text-italic">No Notification Found</a></li> '; $total_row=2; $data

我试图将一个json对象从test.php传递到index.html中的jquery,但我做不到。 下面是我的test.php脚本:

<?php
if(isset($_POST["view"]))
{
  $output .= '
  <li><a href="#" class="text-bold text-italic">No Notification Found</a></li>
  ';
  $total_row=2;

 $data = array(
   'notification'   => $output,
   'unseen_notification' => $total_row
  );


 echo json_encode($data);
}
?>

在PHP中,您需要从服务器中输入标题响应,以便浏览器知道它在响应中得到了什么(请参见:,):

  • 成功
    -请求成功时调用的函数。 参数:
    (任何数据、字符串textStatus、jqXHR、jqXHR)
  • complete
    -当请求完成时(在执行成功和错误回调后)调用的函数。 参数:
    (jqXHR jqXHR,字符串textStatus)
文件:

如果要使用
完成
,则可以通过
jqXHR对象
访问数据(请参阅:):

当然,如果有必要,可以组合不同的事件处理程序

调试时请使用控制台日志(数据)而不是警报(数据)并在浏览器中查看日志。例如,对于Google Chorme(F12,选项卡:Logs):

我在data.responseText中添加了$.trim(),最后它开始工作了(我不知道为什么:)


谢谢大家的帮助

在PHP脚本顶部设置内容类型标题也是个好主意:

<?php
header("Content-Type: application/json");

你的js
success
方法在哪里??顺便说一下,
complete
没有收到数据。(而且,没有JSON对象)成功:函数(数据)警报(data.notification);未执行我通过“var data1=JSON.stringify(data);alert(data1);”更改了此指令“alert(data.notification)”;并且警报显示{“readyState”:4,“responseText”:“?“{”notification\:“
  • 未找到通知“,\”未看到的通知“:2}”,“status”:200,“statusText”:“OK”}@devattitude,检查,返回键所使用的对象:
    success:function(data){console.log(data);}
    或:
    complete:function(jqXHR){if(typeof jqXHR.responseJSON!='undefined'){console.log(jqXHR.responseJSON);}
    @barth thx再次查看下面的我的答案。第一种情况是它无法访问console.log。。InstructionOON和第二种情况:undefined Uncaught TypeError:无法在XMLHttpRequest的z(jquery-2.2.1.min.js:4)处读取Object.fireWith(jquery-2.2.1.min.js:2)处的i(jquery-2.2.1.min.js:2)处未定义的Object.complete((索引):1235)处的属性“通知”。(jquery-2.2.1.min.js:4)@devattitude我不能对你的答案发表评论,我没有这个权利。您需要检查PHP是否返回正确的数据。有必要使用从PHP接收到的所有键准确地检查
    数据
    ,而不是
    数据.notification
    ;var test=JSON.parse(data.responseText);这里是控制台输出:{“通知”:“\n
  • 未找到通知”\n”,“未看到的通知”:2}VM42926:1未捕获的语法错误:JSON中的意外标记位于JSON的位置0处,JSON中位于Object.complete的JSON.parse()处((索引):1235),位于i处(jquery-2.2.1.min.js:2),位于Object.fireWith(jquery-2.2.1.min.js:2),位于z处(jquery-2.2.1.min.js:4)在XMLHttpRequest上。(jquery-2.2.1.min.js:4)@Dygnus,感谢您添加了关于标题的内容。为了完全完成,我在答案中添加了您的添加内容。@devattitude,设置标题,您可以使用
    data.responseText
    而不是
    JSON.parse($.trim(data.responseText))
    <?php
        header("Content-Type: application/json");
    
    $.ajax({
        url: "test.php",
        method: "POST",
        data: {
            view: 10
        },
        dataType: "json",
        success: function(data) {
            alert(data.notification);
        }
    });
    
    complete: function(jqXHR) {
        if (typeof jqXHR.responseJSON !== 'undefined') {
            alert(jqXHR.responseJSON.notification);
        }
    }
    
    var obj = JSON.parse($.trim(data.responseText));
    $('.dropdown-menu').html(obj.notification);
    
    <?php
    header("Content-Type: application/json");