dongying6896 2014-11-25 23:59
浏览 104
已采纳

“[错误] ReferenceError:在尝试使用AJAX / jQuery请求获取JSON数据时找不到变量:data”

I am getting the error "[Error] ReferenceError: Can't find variable: data" when trying to get JSON data with AJAX / jQuery request.

I have an index.html file which loads jQuery and tries to get data from an ajax.php file.

CONTENT of index.html

$.ajax({
    url : 'ajax.php',
    type : 'POST',
    data : data,
    dataType : 'json',
    success : function (result) {
       alert(result['ajax']);
    },
    error : function () {
        alert("error");
    }
    });

CONTENT of ajax.php

$someVariable = array(
    'ajax' => 'Hello world!'
    );
echo json_encode($someVariable);

Accessing ajax.php through the browser displays the data correctly:

{"ajax":"Hello world!"}

What am I missing? Thanks.

  • 写回答

2条回答 默认 最新

  • dtup3446 2014-11-26 00:25
    关注

    I dont know which error you are refering to, but I'm guessing you are enable to read the response of your json response in the success callback, if thats the case you will need to modify the response headers

    $data = /** whatever you're serializing **/;
    header('Content-Type: application/json');
    echo json_encode($data);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!