jquery ajax return: undefined

后端 未结 5 1970
暗喜
暗喜 2020-12-06 11:30

I\'m sure you know this problem, I\'m still trying to solve it for few days. I\'ve tried lots of stuff but no one worked:

Here is the code

function l         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-06 11:49

    I recommend this way.

    1. use the ajax call by the async:false.
    2. move the return statement after the ajax call.

    Example :

    function makeJQGridDataFromList(url)
    {
        var rowData;
        var viewPage = 0;
        var viewTotal = 0;
        var viewRecords = 0;
    
        var resultObject;
    
        $.ajax({    
            type:"GET",
            url:url,    
            async: false,
            success:function(args){ 
                if(args.result==true)
                {
                    try
                    {
                        viewPage = args.cond.pageIndex;
                        viewTotal = args.cond.recordCountPerPage;
                        viewRecords = args.cond.totalCnt;
    
                        rowData = jsonMakeRowsForGrid(args.data);
                    }
                    catch (e)
                    {
                        console.debug("Error!");
                        alert("Invalid data");
                        return;
                    }
                } else
                {
                    alert("API return ERROR!");
                    return;
                }
            }, 
            error:function(e){
                alert("Fail AJAX communication");
                return;
            }
        });
    
        resultObject = {
            page : viewPage,
            total : viewTotal,
            records : viewRecords,
            rows : rowData
        };
    
        return(resultObject);
    }
    

    You can test the following method.

    (In the other file (html or js))

    var gridData = makeJQGridDataFromList(openAPIUrl);
    console.debug(">> " + JSON.stringify(gridData));
    

    You can see the gridData.

    I faced same problems. :)

提交回复
热议问题