PHP函数不向JQuery ajax方法返回任何数据 [英] PHP function not returning any data to JQuery ajax method

查看:125
本文介绍了PHP函数不向JQuery ajax方法返回任何数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用jquery的ajax方法将一些数据发布到服务器并获取响应.尽管服务器端php代码返回的是json编码的字符串/数组,但响应返回为null.

I am using jquery's ajax method to post some data to the server and get back the response. Though the server side php code is returning a json encoded string/array, the response is coming back as null.

有人能指出我犯的错误吗?下面是我的jquery ajax方法用来访问postData.php页面的情况.

Could someone point out the mistake that I am making. Below if my jquery ajax method using which I am hitting the postData.php page.

        $.ajax({
            url:'postData.php',
            type:'POST',
            data:data,
            dataType: "json",
            success: function(response){
                console.log(response);
            }
        });

postData.php中的内容非常简单,因为我仍在开发中.

The content in postData.php is pretty straight forward as I am still developing it.

    $data = array();
//inside postData.php
    $data['test']=1;
    return json_encode($data);

它应该返回一个json字符串,但是它返回null.我也尝试在$ data数组声明之后回显字符串,它确实在萤火虫中回显它,但是响应是当我在成功回调上执行console.log时,它返回为null.

It should return a json string, but it is returning null. I also tried echoing a string just after $data array declaration, it does echo it in the firebug, but the response is when I do a console.log on the success callback, it comes back as null.

推荐答案

是postData.php中的全部内容吗?您需要在某个时候将其写出到缓冲区(回显json_encode($ data);).

Is that all that is in postData.php? You need to write it out to the buffer (echo json_encode($data);) at some point.

这篇关于PHP函数不向JQuery ajax方法返回任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆