weixin_33737774 2016-01-08 01:22 采纳率: 0%
浏览 38

JavaScript和范围问题

I have an issue with calling ajax request with jquery. The order that I'm doing this in is:

  1. click the button
  2. do ajax post
  3. when the ajax request is finished I call a function that is out side the scope.

For some reason and supecting that it has to do with the fact that i am in the on click callback that the load function is out of scope. But I don't even see the console.log message either. But I do see the ajax call.

Any ideas? Maybe I'm doing this the wrong way???

Here's the prototype code that resembles what I'm trying to do:

$(document).ready(function(){

        $('#button').on('click',function(evt){
            var data = {};

            ajax('index.html', data).done(function(){
                console.log('Fire Please'); // this does not fire after the ajax call!!!
                    load(); // this does not fire after the ajax call!!!

            });
        });

        function load(){
            // do another ajax call and add to the dom
        }

        function ajax(url, data){
            return $.ajax({
                url: url,
                type: 'post',
                dataType: 'json',
                data: data
            });
        }
});

And Here's the Actual Code I'm trying to use

$(document).ready(function(){

    // add onclick event to the Add Unit Button
addUnitButt.on('click', function(evt){
    var data = {
        id: id,
        dept_no: dept_no.val(),
        dept: dept.val()
    };

    evt.preventDefault();

    dept.val('');
    dept_no.val('');
    $(this).prop('disabled', true);


    ajax('index.html', data).done(function(){
        load();
    });

});

function load(){
    var data = {
        id: 575
    };

    // show loading
    showLoading();

    // reset the table dom
    $("#listTable").find("tr:gt(0)").remove();

    // do initial load of the list data
    ajax('index.html', data)
    .done(function(units){
        var data = toJSONObject(units);

        for(var x = 0; x < data.length; x++){
            if((x & 1) == 0){
                addRow(data[x], data.length, 'odd');
            }else{
                addRow(data[x], data.length, 'even');
            }
        }

        // hide loading
        hideLoading();
    });
}

// ajax function to call for data
function ajax(url, data){
    return $.ajax({
        type: 'POST',
        data: data,
        dataType: 'json',
        url: url
    });
}

});

Thanks in advance!

  • 写回答

2条回答 默认 最新

  • from.. 2016-01-08 01:44
    关注

    .always() must be called.

    Make sure your server response have the right headers like Content-Type. And the response body is valid JSON.

    评论

报告相同问题?

悬赏问题

  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd