简体   繁体   English

JSON使用jQuery $ .ajax响应时返回null

[英]JSON returning null on response with jQuery $.ajax

I'm trying to get response on ajax post but returning NULL always instead. 我试图在ajax发布上获得响应,但总是返回NULL。

I'm thinking that already try everything that I can... but hope somebody will be able to solve it. 我以为已经尝试了所有可能的方法...但是希望有人能够解决它。 Thanks very much in advance. 首先十分感谢。 And if needs more information please tell me. 如果需要更多信息,请告诉我。

NOTE: PHP function not returning any data to JQuery ajax method - this link didn't help me 注意: PHP函数不会向JQuery ajax方法返回任何数据 -此链接对我没有帮助

Here is jquery code: 这是jQuery代码:

var post_id=3;
        $.ajax({

            type: "POST",
            url: "profile/ajax_get_new_posts",
            data: { post_id : post_id },
            dataType: 'json',
            success: function(data){

                console.log(data);
                var current_content = $("ol#posts_container").html();

                $("ol#posts_container").html(data.content + current_content);
            }


        });

profile/ajax_get_new_posts (it is .php) profile / ajax_get_new_posts(.php)

function ajax_get_new_posts(){

        header('Content-Type: application/json');
            $post_id=$this->input->post('post_id');
            $chat_messages_html='testing';
            $result = array('status' => 'ok', 'content' => $chat_messages_html);
            $result=json_encode($result);
            return ($result);
            exit();
        }

If I try with var_dump($result);die(); 如果我尝试使用var_dump($ result); die(); gives me correct result: 给我正确的结果:

string(38) "{"status":"ok","content":"samoletite"}"

On the Response Headers gives me Content-Length:0 (it is testing on localhost... not www.mywebsite) 在响应标头上,我得到Content-Length:0(正在localhost上进行测试...,而不是www.mywebsite)

 Remote Address:[::1]:80
    Request URL:http://www.mywebsite/bg/profile/ajax_get_new_posts
    Request Method:POST
    Status Code:200 OK
    Request Headersview source
    Accept:application/json, text/javascript, */*; q=0.01
    Accept-Encoding:gzip,deflate
    Accept-Language:en-US,en;q=0.8
    Connection:keep-alive
    Content-Length:9
    Content-Type:application/x-www-form-urlencoded
    Host:localhost
Origin:http://localhost
Referer:http://www.mywebsite/bg/profile
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
X-Requested-With:XMLHttpRequest
Form Dataview sourceview URL encoded
post_id:3
Response Headersview source
Connection:Keep-Alive
Content-Length:0
Content-Type:application/json
Date:Fri, 31 Oct 2014 13:24:56 GMT
Keep-Alive:timeout=5, max=97
.......

Console.log gives me: Console.log给我:

null --->ajaxPostProfile.js:28
Uncaught TypeError: Cannot read property 'content' of null --->ajaxPostProfile.js:31

Instead to use return ($result); 而是使用return ($result); , use echo . ,使用echo Like this: 像这样:

function ajax_get_new_posts(){

        header('Content-Type: application/json');
            $post_id=$this->input->post('post_id');
            $chat_messages_html='testing';
            $result = array('status' => 'ok', 'content' => $chat_messages_html);
            $result=json_encode($result);
            echo $result;
            exit();
        }

And test again. 并再次测试。

you can return json data in PHP using following syntax: 您可以使用以下语法在PHP中返回json数据:

  header('Content-Type: application/json');
  echo json_encode($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM