perhaps? 2013-07-05 13:14 采纳率: 100%
浏览 33

jQuery Ajax响应错误

Here is a partial example of what I am trying to achieve.

I am trying to retrieve a value from ajax but the javascript result.success variable is undefined. I have the following:

PHP:

$result = array ("success" => null, "html" => "");
$result['success'] = true;
$result['html'] = "test";
echo json_encode($result);

Javascript/jQuery:

var ID = 1
$.ajax({
            type: 'POST',
            url: '/ajax/load.php',
            contentType: "application/json; charset=utf-8",
            datatype: 'json',
            data: {ID: ID},
            beforeSend: function(xhrObj) {
                xhrObj.setRequestHeader("Content-Type","application/json");
                xhrObj.setRequestHeader("Accept","application/json");
            },
            success: function(result) {
                if (result.success) {
                        // do something
                }
        });

The response I am getting from ajax (retrieved from chrome dev tools) is {"success":true,"html":"Test"}

This looks fine to me however in the JavaScript result.success is undefined. I believe this will be simple I just can't see where the issue lies..

  • 写回答

3条回答 默认 最新

  • weixin_33712881 2013-07-05 13:17
    关注
    $.ajax({
                type: 'POST',
                url: '/ajax/load.php',
                contentType: "application/json; charset=utf-8",
                dataType: 'json',
                data: {ID: ID},
                beforeSend: function(xhrObj) {
                    xhrObj.setRequestHeader("Content-Type","application/json");
                    xhrObj.setRequestHeader("Accept","application/json");
                },
                success: function(result) {
                    if (result.success) {
                            // do something
                    }
                } // Maybe your forgot this
            });
    

    in other words - Basic Debugging

    评论

报告相同问题?

悬赏问题

  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件