【问题标题】:AJAX function not recognising json returnAJAX 函数无法识别 json 返回
【发布时间】:2014-01-20 18:59:35
【问题描述】:

我正在通过发送回课程时间表的 HTML 表单调用在线 API。我在初始页面加载时填充时间表,然后使用 jQuery 和 AJAX 来获取过滤的类。

我遇到的问题是我的 AJAX 函数无法识别作为 JSON 发送的数据。我确保使用 PHP 函数中的“json_encode”发回数据,并且 jQuery 中的“dataType”是“json”。 因此,AJAX 函数的“成功”回调中没有运行任何代码。这是我的代码:

AJAX:

jQuery(document).ready(function() {
    jQuery(document).on("change", "#calendar-filter-form select", function() {
        jQuery.ajax({
            type      :   'POST',   
            dataType  :   'json',
            data      :   jQuery("#calendar-filter-form").serialize(),
            url       :   "<?php echo $_SERVER['PHP_SELF']; ?>",
            success   : function(data) {
                if(data) {  
                    alert("test");
                    var html = "";
                    jQuery(data.results).each(function(i, v) {
                        alert(v);
                        html += '<div class="day">';
                        html += '<h3>' + v.day + '</h3>';
                        jQuery(v).each(function(i2, v2) {
                            html += '<div class="activity-item">';
                                html += '<h4>' + v2.SessionName + '</h4>;'
                                html += '<p class="session-time">Time: ' + v.Time + 'Start</p>';
                                html += '<p class="remaining-capacity">Remaining Spaces: ' + v.RemainingCapacity + '</p>';
                            html += '</div>';
                        });
                        html += '</div>';
                    });
                    jQuery("#calendar").html(html);
                }
            }           
        });
    });
});

以及调用 API 并返回数据的 PHP:

if(isset($_POST['filterClasses'])) {
    $url = '/classes/search?FacilityId=1';
    $options = array();
    foreach($_POST as $key => $postItem) {
        if(!empty($postItem)) {             
            $url .= "&ActivityName=" . urlencode($postItem);                
            $options['url'] = $url;             
        }
    }
    $results = makecURLRequest($options);
    $resultsSorted = array();
    $resultsDecoded = json_decode($results);
    $filters = array();
    $resultsSorted = array();
    $days = array('Monday' => 1, 'Tuesday' => 2, 'Wednesday' => 3, 'Thursday' => 4, 'Friday' => 5, 'Saturday' => 6, 'Friday' => 7);
    foreach($days as $key => $day) {
        $resultsSorted[$day] = array();
        $resultsSorted[$day]['day'] = $key;     
        foreach($resultsDecoded as $result) {       
            if($result->DayOfWeek == $day) {
                $timestamp = strtotime($result->Time);
                array_push($resultsSorted[$day], $result);
            }                           
        }
    }   
    header('Content-Type: application/json', true);
    echo json_encode(array("results" => $resultsSorted));
}

这是从 API 返回的 JSON:

 [
    {
        "ActivityInstanceId":11959,
        "FacilityId":1,
        "SessionName":"Swim Scheme Lessons",
        "GroupId":null,
        "DayOfWeek":4,
        "Time":"2014-01-23T16:00:00",
        "RemainingCapacity":1,
        "Description":null,
    },
    {
        "ActivityInstanceId":11959,
        "FacilityId":1,
        "SessionName":"Swim Scheme Lessons",
        "GroupId":null,
        "DayOfWeek":4,
        "Time":"2014-01-23T16:00:00",
        "RemainingCapacity":1,
        "Description":null,
    },
...

我似乎不明白为什么这没有被识别为有效的 JSON。谁能指出我正确的方向? 谢谢

【问题讨论】:

  • 你能在 PHP 页面上显示实际的输出吗?结果中某处的对象 (json_encode) 有时会破坏 JSON 字符串的结构。
  • 当然。 PHP 中返回点的输出还是返回到 AJAX 函数的输出?
  • PHP 之一。所以我们可以读取原始的 JSON 字符串。
  • 控制台中是否有任何错误,您是否尝试在 ajax 调用中添加错误处理程序以查看内容等?
  • 好的,我已经添加了输出。谢谢

标签: php jquery ajax json


【解决方案1】:

您的 json 无效。

{
        "ActivityInstanceId":11959,
        "FacilityId":1,
        "SessionName":"Swim Scheme Lessons",
        "GroupId":null,
        "DayOfWeek":4,
        "Time":"2014-01-23T16:00:00",
        "RemainingCapacity":1,
        "Description":null ***,***
    },

假设是:

{
            "ActivityInstanceId":11959,
            "FacilityId":1,
            "SessionName":"Swim Scheme Lessons",
            "GroupId":null,
            "DayOfWeek":4,
            "Time":"2014-01-23T16:00:00",
            "RemainingCapacity":1,
            "Description":null
        },

最起码不需要","的元素。

并尝试添加

if(data) { data = JSON.parse(data ) }

在你的 js 中。 它应该可以工作。

【讨论】:

    【解决方案2】:

    我弄清楚发生了什么。我将 AJAX 请求提交到同一页面(向 html 标记上方的 php)提交,它试图再次重新加载整个页面,包括在初始页面加载时发送的 php 变量。这导致 AJAX 功能无法正常工作。 我通常在 MVC 框架中工作,但这次不是,所以它让我有点失望。 希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-05-20
      • 1970-01-01
      • 1970-01-01
      • 2019-11-18
      • 1970-01-01
      • 1970-01-01
      • 2011-07-10
      • 2017-06-04
      • 1970-01-01
      相关资源
      最近更新 更多