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 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器