Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
jqueryajax在javascript中获得与PHP不同类型的变量_Javascript_Php_Jquery_Ajax - Fatal编程技术网

jqueryajax在javascript中获得与PHP不同类型的变量

jqueryajax在javascript中获得与PHP不同类型的变量,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,在我的php代码中,我回显变量的对象类型。然而,在JQuery中,ajax成功返回的数据是字符串数据的形式。我使用JSON.parse(data)将数据解析为JSON格式。但是由于返回字符串数据的格式,我不能这样做。我想知道如何在php中返回JSON对象,同时在JQuery$ajax函数中也会得到JSON对象,而不是字符串。下面是我的代码 Javascript: $(document).ready(function callAjax(){ $.ajax({

在我的php代码中,我回显变量的对象类型。然而,在JQuery中,ajax成功返回的数据是字符串数据的形式。我使用JSON.parse(data)将数据解析为JSON格式。但是由于返回字符串数据的格式,我不能这样做。我想知道如何在php中返回JSON对象,同时在JQuery$ajax函数中也会得到JSON对象,而不是字符串。下面是我的代码

Javascript:

$(document).ready(function callAjax(){
        $.ajax({
            type: "GET",
            url: "php/test.php",
            cache: false,
            success: function(data){
                console.log( data);
                 interval = setTimeout(callAjax, 1000);  
            }
        })
});
PHP:


在ajax调用中添加
数据类型:'json'

你必须让他知道你从ajax调用中得到了什么样的数据

json_encode(array('state'=>'nothing'));
php代码

 require('test2.php');

   $messages = get_msg();
    if (is_array($messages) || is_object($messages)){
        foreach($messages as $message){
      $array = array('chat_id' => $message['chat_id'], 
                    'sender_name' => $message['sender_name'],
                    'chat_body' => $message['chat_body'], 
                    'chat_time' => $message['chat_time']);
      $object = (object) $array;
            echo json_encode(array('state'=>gettype($object)));
        }
    }else{
        echo json_encode(array('state'=>'nothing'));
}
Javascript

$(document).ready(function callAjax(){
        $.ajax({
            type: "GET",
            url: "php/test.php",
            cache: false,
            dataType:'json',
            success: function(data){
                 try {
                    data = jQuery.parseJSON(data);
                  }
                  catch (err) {
                     data = typeof data == 'object' ? data : jQuery.parseJSON(data);
                }
                 console.log(data.state);
                 interval = setTimeout(callAjax, 1000);  
            }
        })
});

谢谢你的评论。但我只是在javascript中添加了数据类型:'json'。$。ajax,程序无法加载成功函数。为什么我们必须添加echo json_encode(数组('state'=>$object))?为什么javascript中的json_encod($object)ajax函数无法加载json对象?您一定错过了后跟此
'json'的逗号,
plus数组被传递,因为它们被认为是很好的实践,就像您可以将多个内容从php传递到ajax。请使用我的代码,我已经更改了许多内容,我很确定。它会起作用的。谢谢!不,我没有遗漏任何东西。但我想知道为什么在添加数据类型之后添加“json”。Ajax功能不起作用。您能用您尝试过的代码和出现的错误进行更新吗@chanyoonghon@chanyoonghon检查我的最新答案,我已经为你添加了一个try catch,看看它是否有帮助,如果有效,请让我知道。谢谢。
$(document).ready(function callAjax(){
        $.ajax({
            type: "GET",
            url: "php/test.php",
            cache: false,
            dataType:'json',
            success: function(data){
                 try {
                    data = jQuery.parseJSON(data);
                  }
                  catch (err) {
                     data = typeof data == 'object' ? data : jQuery.parseJSON(data);
                }
                 console.log(data.state);
                 interval = setTimeout(callAjax, 1000);  
            }
        })
});