weixin_33749131 2016-06-21 18:08 采纳率: 0%
浏览 46

从ajax调用返回值

I'm trying to get the return bool from the ajax call, but when I run the function and save to a variable but the only thing i get is undefined.

function check(number, id){
    $.ajax({
        url: 'ai.php',
        data: {
            "func":"checkNumber",
            "id":id,
            "number":number
        },
        method: 'GET',
        dataType: 'json'
    }).done(function(jData) {
        return jData['idFound'];
    }).fail(function(jData) {
        console.log(jData['idFound']);
    });
}

var test = check(44444, 12);
console.log(test);
// true

// but i get an undefined value in the console.log
  • 写回答

2条回答 默认 最新

  • weixin_33721427 2016-06-21 18:19
    关注

    Depend ur file 'ai.php' is a format json? please check validate http://www.jsonlint.com

    Confuse ur code data, its just 'post' and not 'get'.

    You can try getjson or ajax, look a exemple:

    $(document).ready(function() {
    
        $.getJSON('http://beta.json-generator.com/api/json/get/NkmG8_zBZ', function(json) {
            for (x=0; x<json.length; x++) {
              console.log('getjson>'+json[x].nome);
            }
        });
    
        $.ajax({
            type: 'get',
            url: 'http://beta.json-generator.com/api/json/get/NkmG8_zBZ',
            dataType : 'json',
            success: function(json) {
              for (x=0; x<json.length; x++) {
                console.log('ajax>'+json[x].cidade);
              }
            },
            error: function(err) {
                console.log(err);
            }
        });
      
      });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?