Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/418.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 回调未定义_Javascript_Jquery_Callback - Fatal编程技术网

Javascript 回调未定义

Javascript 回调未定义,javascript,jquery,callback,Javascript,Jquery,Callback,在这段代码中,我有一个回调未定义错误: function getPacakage(callback){ var sendurl={address:'http://ip/bms/data.php?result=10&order=28'} var temp_selpkg_group=[]; var temp=[]; $.ajax({ type:"GET", url:"dummycall.php", data:sendurl,

在这段代码中,我有一个
回调未定义
错误:

function getPacakage(callback){
var sendurl={address:'http://ip/bms/data.php?result=10&order=28'}
var temp_selpkg_group=[];
var temp=[];

    $.ajax({
        type:"GET",
        url:"dummycall.php",
        data:sendurl,

        success:function(xml){              

            pkg_sel[0] = $(xml).find('test1').text();
            pkg_sel[1] = $(xml).find('test2').text();
            pkg_sel[2] = $(xml).find('test3').text();
            $(xml).find('packageitem').each(function(){
                temp_selpkg_group=[];
                temp_selpkg_group[0]=$(this).find('group').text();
                temp_selpkg_group[1]=$(this).find('Qty').text();
                temp[cnt_pkgitem]=temp_selpkg_group;
                cnt_pkgitem++;                  
            });

            callback.call(null,temp);
        },//sucess ends

        error: function(){
            alert("An error occurred while processing XML file.");
        }   //error ends..  

    }); //ajax ends...  

}
下面是我调用函数的方式:

$(document).ready(function (){
    $(function() {
        $( "#accordion" ).accordion();
    });     

    getPacakage(function(temp){
        sel_pkg_group=temp;
        fillOptionList();
    });

});//document ready ends..

如果我刷新页面(Ctrl+F5)2到3次,则显示数据。

删除以下行-

callback.call(null,temp);

由于Ajax是一个asyc任务,我们需要首先完成一个Ajax任务,然后调用函数来使用Ajax中初始化的数组,因此只需在Ajax中添加async:false

 $.ajax({
        type:"GET",
        url:"dummycall.php",
        data:sendurl,
        async: false,
        success:function(xml){  ..............
这将停止异步和其他方法将被调用时,只有一个完整的,这对我的工作可能会帮助我的其他朋友,快乐的编码