Ajax使用回调函数返回值

时间:2014-04-21 15:32:45

标签: javascript jquery ajax

我修改了我的代码以使用回调函数来返回值,但我认为仍然存在错误。我无法从我的回调中访问该值,即使它在控制台日志中可以看到它的返回值。

的Javascript

function postToServer(ajaxSuccessFn) {
    var Employee = {
        //some data
    };
    console.log('inside post to server');

    try {
        dojo.xhrPost({
            url: 'hello',
            content: Employee,
            load: ajaxSuccessFn,
            error: function (e) {
                console.log('Error Occured ' + e);
            }
        });

    } catch (e) {
        console.log(e);
    }

};

function handleServerResponse(containers, Callback) {

    var formErrors = {
        "errors": "yes",
        "firstName": "1234",
        "surname": "456",
        "otherNames": "789",
        "bSurname": "10025"
    };

    console.log('handling server error obj');

    try {
        $.each(formErrors, function (key, value) {

            if (key == 'errors') {

                hasErrors = value;
                console.log('hasErrors set to ' + value);
            }
        });

    } catch (e) {
        console.log(e);
    }

    try {
        if (hasErrors == true) {
            //alert('form has errors');
            for (var i = 0; i < containers.length; i++) {

                console.log('Processing container number ' + i + ' with name ' + containers[i]);
                var processingContainer = containers[i];

                dojo.forEach(processingContainer.getChildren(), function (wid) {
                    var widgetName = wid.attr('id');


                    $.each(formErrors, function (key, value) {

                        if (key == widgetName && value.length > 0) {

                            var myWidget = dijit.byId(widgetName);
                            var wdgName = dijit.byId(widgetName).attr("id");
                            var myWidgetValue = value;
                            //console.log('Printing here '+wdgName +' : '+myWidgetValue);

                            myWidget.validator = function () {
                                console.log('Attribute Name is :' + wdgName + ' Error Value is : ' + myWidgetValue);
                                //console.log(wdgName + " : "+myWidgetValue);
                                this.set("invalidMessage", myWidgetValue);
                            };
                            myWidget._hasBeenBlurred = true;
                            myWidget.validate();

                        }

                    });

                });

                console.log('done ' + hasErrors);
            }
        }

    } catch (e) {
        console.log(e);
    }

    console.log('function returning hasErrors as ' + hasErrors);
    return hasErrors;
}

function PostNameInformation() {

    var containers = [byId("container1"), byId("container2")];

    try {
        var doesErrorsExist = postToServer(handleServerResponse(containers, function (args) {
            return args;
        }));
        console.log('Were there any errors ' + doesErrorsExist);
    } catch (e) {
        console.log(e);
    }
}

0 个答案:

没有答案