weixin_33713503 2016-02-16 03:25 采纳率: 0%
浏览 6

通过回电返回ajax? [重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call" dir="ltr">How do I return the response from an asynchronous call?</a>
                            <span class="question-originals-answer-count">
                                (38 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2016-02-16 03:33:55Z" class="relativetime">4 years ago</span>.</div>
        </div>
    </aside>

I known that ajax success function is can't return data,so I had tried by call back function,but it still there getting undefined return.I have done by variable responseText ,but I'm not want to do by that.So help me with callback function return data!!!

HTML

<div id="address">
        <table>
        <thead>
        <th>Department</th><th>Name</th><th>Age</th><th>Address</th>
        </thead>
        <tbody>
        </tbody>
</div>

JS

getAddress callback() is return null

 $(document).ready(function(){
        $.ajax({
            url : "header.php",
            dataType : "json",
            success : function (d) {
            var temp = getAddress(callback); 
            console.log(temp)   // undefined
            var data = JSON.parse(temp);
            $.each(data,function(i,d){
                $("#address tbody").append("<tr><td>"+d[i].dept_name+"</td><td>"+d.Name+"</td><td>"+d.Age+"</td><td>"+d.Address+"</td></tr>");
            });         
            }
        });                     
    });    

     function callback(result){
        return result;
     }
    function getAddress(callback){
           $.ajax({
           url: 'address.php',
           async : false,
           dataType : 'json',       
           success: function(result){
            callback(result);
           }
           });          
    }
</div>
  • 写回答

1条回答 默认 最新

  • weixin_33709219 2016-02-16 03:28
    关注

    You are using the callback in a wrong way. The logic working with the value returned by the ajax call should be within the callback

    $(document).ready(function() {
      $.ajax({
        url: "header.php",
        dataType: "json",
        success: function(d) {
          getAddress(function(data) {
            console.log(data) // undefined
            //var data = JSON.parse(temp); not required as you have `dataType: 'json'
            $.each(data, function(i, d) {
              $("#address tbody").append("<tr><td>" + d[i].dept_name + "</td><td>" + d.Name + "</td><td>" + d.Age + "</td><td>" + d.Address + "</td></tr>");
            });
          });
        }
      });
    });
    

    In your code, you are calling getAddress with the callback, but since the getAddress method is not returning anything the value of temp will be undefined.

    评论

    报告相同问题?

    悬赏问题

    • ¥15 存储过程或函数中的结果集类型变量如何使用。
    • ¥80 关于海信电视聚好看安装应用的问题
    • ¥15 vue引入sdk后的回调问题
    • ¥15 求一个智能家居控制的代码
    • ¥15 ad软件 pcb布线pcb规则约束编辑器where the object matpcb布线pcb规则约束编辑器where the object matchs怎么没有+15v只有no net
    • ¥15 虚拟机vmnet8 nat模式可以ping通主机,主机也能ping通虚拟机,但是vmnet8一直未识别怎么解决,其次诊断结果就是默认网关不可用
    • ¥20 求各位能用我能理解的话回答超级简单的一些问题
    • ¥15 yolov5双目识别输出坐标代码报错
    • ¥15 这个代码有什么语法错误
    • ¥15 给予STM32按键中断与串口通信