【问题标题】:PHP echoing emptying string in $.ajax responsePHP 在 $.ajax 响应中回显清空字符串
【发布时间】:2014-10-28 16:16:55
【问题描述】:

我正在尝试从 AJAX 调用中取回数据。我用一个字符串进行了测试,但它返回为空。我只想在响应时将字符串打印到控制台。

JS:

makeRequest: function(sService, oData){
        var request = $.post(
            '../../Utils/utils/php/userQuery.php'
            ,{'sService' : sService, 'oData':  oData}
            ,'json'
        );
        request.done(function(oResult){
            // This is firing off, but is printing empty string
            console.log(JSON.stringify(oResult));
            var fail = errorHandler.check(oResult);
            if(!fail){
                // This is firing off, but is printing empty string
                console.log(oResult);
            } else{
                console.log(fail);
            }
        });
        request.fail(function(oResult){
            console.log("There was an error in retrieving service data");
        });
    }

PHP:

class request{
    private $sService;
    function _construct($sService){
        $_SESSION['user_Id'] = 100000;
        $this->$sService = $sService;
        switch($this->$sService){
            case 'follower':
                break;
            case 'followee':
                break;
            case 'promoter':
                break;
            case 'promotee':
                break;
            case 'note':
                // This should be hit and return "hit"
                echo json_encode("hit");
                break;
            case 'createFollowee':
                break;
            case 'createPromotee':
                break;
            case 'createNote':
                break;
            default:
                echo "ErrorCode: 4000";
                break;
        }
    }
}
$request = new request($_POST['sService']);

如果我尝试$.parseJSON(oResult) 响应,我会收到解析错误,因为它是空的。我哪里出错了?

回答:

上面更新的代码有效。答案提供了几个问题。

  1. 我没有在construct()中添加2个_。[之前:_construct 之后:__construct]
  2. 我没有 $this 指向我的类中的全局变量。
  3. 我最初没有初始化我的类。
  4. 初始化类时,我没有将变量传递给构造。

我希望这对其他人有帮助。

【问题讨论】:

  • 该类request的实例在哪里?
  • 您是否检查过浏览器开发人员工具以检查请求是否确实为'sService' 传输了正确的值,并在对结果进行字符串化之前检查结果?仅供参考:在登录到控制台之前不需要 stringify。
  • 我也认为你想这样做$this->sService = $_POST['sService']; 有了你所拥有的,我希望“错误代码:4000”得到回应($this->sService 目前为空,除非你改变我的建议)
  • echo(json_encode('hit')); 您要求返回 json,但您正在回显纯文本。只需将其进行 json 编码,您就可以开始了
  • 作为@JensonMJohn 所说的延续,我看不到您在哪里初始化了request 类。您需要使用new request(); 初始化该类。链接到该文件,或使用另一个 php 文件中的 includerequire 不会自动初始化您的类。在文件底部添加new request();...

标签: javascript php jquery ajax


【解决方案1】:
  1. 使用浏览器的开发工具检查服务器响应。也许响应真的是空的?
  2. 您想获取 JSON 数据,但您没有 json_encode 您的输出
  3. PHP 构造函数以 2 下划线开头:__construct(不是_construct
  4. $sService = $_POST['sService']; 应该是 $this->sService = $_POST['sService'];
  5. 当你不使用new request();初始化你的类时,构造函数不会被调用

【讨论】:

    【解决方案2】:

    这可能是您的类构造函数运行不正常。声明构造函数的正确语法是function __construct()两个下划线前面)或function theClassName()。检查the PHP manual

    http://codepad.viper-7.com/k3vclY - 无/空输出,带有一个下划线的 _construct()
    http://codepad.viper-7.com/vabqqL - 带有两个下划线的 __construct() 的输出

    【讨论】:

      猜你喜欢
      • 2016-03-16
      • 2017-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-16
      • 2015-07-02
      • 2019-10-28
      相关资源
      最近更新 更多