开发者

Trying to simulate an ajax response in jquery

开发者 https://www.devze.com 2023-04-01 04:53 出处:网络
I\'m trying to use jsFiddle to simulate an ajax response but I\'m getting an undefi开发者_C百科ned value. Does anyone know how to do this? My fiddle is here:

I'm trying to use jsFiddle to simulate an ajax response but I'm getting an undefi开发者_C百科ned value. Does anyone know how to do this? My fiddle is here:

http://jsfiddle.net/heaversm/v75vN/10/

and to see the problem click on "login" from the result box


You are returning from $.ajax's callbacks (success and error). Since the $.ajax method give you callback hooks, you should provide the same in the loginUser function

loginUser: function(cidVal, midVal, surveyVal, callback) {

    $.ajax({
        // ...
        success: function(data) { callback(true); },
        error: function(data){ callback(false);},
    });
}

And then use that callback to know when the operation has completed:

loginUser('foo', 'bar', 'far', function (result) {
    alert(result);
});


Your alert statement runs before the Ajax request has completed. You need to pass in a callback to updateUser to set loginResult:

updateUser: function(cidVal,aidVal,sidVal,surveyVal, onSuccess){
    $.ajax({
        url: wsURL,
        type: 'POST',

        //data: { Function: "UpdateConsumer", ConsumerId: cidVal, ActivityId: aidVal, SurveyId: sidVal, Survey: surveyVal }
        //Sample XML Data for the Purpose of Testing in JS Fiddle:
        data: { xml: '<UpdateConsumer><Status>OK</Status></UpdateConsumer>' },
        success: function(data) {
            if (onSuccess) {
                onSuccess(data);
            }
        },
        error: function(data){
            //Error in data or unable to connect
            return ("error");
        },
        dataType: "xml" //We're expecting XML back from the server
    });

Then to call it:

loginResult = c.loginUser(cid,mid,survey, function (result) { alert(result); } );
0

精彩评论

暂无评论...
验证码 换一张
取 消