【问题标题】:AJAX call returning empty object from json_encodeAJAX 调用从 json_encode 返回空对象
【发布时间】:2014-11-13 02:17:27
【问题描述】:

我正在对一个相当简单的 PHP 函数进行 AJAX 调用。响应应该是要操作的 JSON 对象,但我的响应始终是空对象。

相关代码:

index.html 的 AJAX 调用:

$( function() {
    $('#dates').on('submit', function (e) {
        var start_time = new Date().getTime();
        e.preventDefault();
        var submitData = $('#dates').serialize();
        console.log(submitData);
        $.ajax({
            type:'POST',
            url:'inflow.php',
            data:$('#dates').serialize(),
            dataType: 'json',
            beforeSend: function(){
                $('#loading').show();
            },
            success: function(data) {
                console.log(data);
                $('#loading').hide();
            },
            error:function(xhr, desc, err){
                alert('You tried to send an AJAX request, but something went wrong.\n Please Contact the NASR WebDev Team');
                console.log(xhr);
                console.log("Details: " + desc +"\nError: "+ err);
            }
        });
    });
});

inflow.php 的数组创建和回显:

<?php

$msqlStart = $_POST['start_date'];
$msqlEnd = $_POST['end_date'];

$inflowQuery=
    "SELECT
        COUNT,
        QUEUE_ID,
        QUEUE_GROUP,
        INFLOW_DATE,
        LINE_OF_BUSINESS
    FROM
        ticket_inflow.inflow
    WHERE
        ROUTE_STATUS = 'Inflow'
        AND inflow_date between ? and ?";

    $connect = new mysqli($host, $user, $password, $database);
    if ($connect->connect_error){
        die("Failed to Connect:.".$connect->connect_errno.": ".$connect->connect_error);
    }
    if(!($statment = $connect->prepare($inflowQuery))){
        die('Error in Preparation Error Number:%d Error: %s');
    }
    if(!$statment->bind_param('ss', $msqlStart, $msqlEnd)){
        die ('Error in Binding');
    }
    if(!$statment->execute()){
        die('Error In Execution');
    }
    $statment->bind_result($inflowCount, $inflowQueue, $inflowQG, $inflowDate, $inflowLOB);

    $a_json = array();
    $jsonRow = array();

    While($statment->fetch()){
        $UID = 0;
        $jsonRow['UID'] = $UID++;
        $jsonRow['count'] = utf8_encode($inflowCount);
        $jsonRow['inflow_date'] = utf8_encode($inflowDate);
        $jsonRow['queue'] = utf8_encode($inflowQueue);
        $jsonRow['queue_group'] = utf8_encode($inflowQG);
        $jsonRow['lob'] = utf8_encode($inflowLOB);
        array_push($a_json, $jsonRow);
    }


    $jsonReturn = json_encode($a_json);

    echo $jsonReturn; 

?>

如果我直接转到 inflow.php 并将其传递的参数与页面传递的参数相同,我会得到一个看起来不错的 JSON 对象,但是当我查看 Chrome Developer's Tools 的响应时,我得到:

[]

仅此而已。

【问题讨论】:

  • 看起来你的 $statment->fetch() 没有触发 while
  • 你为什么使用contentType: "application/json"?你正在发送一个 url 编码的文本字符串,所以你不应该使用它。
  • @Rooster 这是我最初的想法,然而,直接访问 php 页面会回显看起来像一个有效的 JSON 对象。

标签: php ajax json pivot.js


【解决方案1】:

您发送的是表单数据,而不是 json;

$.ajax({
        type:'POST',
        url:'inflow.php',
        data:$('#dates').serialize(),
        //contentType: "application/json",<-- remove this
        dataType: 'json',

【讨论】:

  • 我认为这是我的故障排除遗留问题。即使删除了它,我仍然得到一个空数组。
  • 我添加了完整的 PHP,减去了服务器的实际登录凭据。当我用充满垃圾的 $testRows 触发相同的时候,它似乎正确地传递了 JSON。
  • @Sugarcaen 你说这个页面(inflow.php)在浏览器中直接访问时会返回结果吗?
  • 是的。当我转到 inflow.php?start_date&end_date 时,我得到了似乎是有效的 JSON 对象(至少 JSON Lint 通过了它。)
  • @Sugarcaen 在浏览器中访问该页面将导致 $msqlStart$msqlEnd 成为 null$_POST 对于获取请求将为空),所以我希望您的参数是通过 ajax 发送的格式不正确 - 您是否尝试在 php 中对值进行硬编码以进行测试
猜你喜欢
  • 1970-01-01
  • 2012-12-05
  • 2019-04-10
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-28
相关资源
最近更新 更多