开发者

Problem with jQuery Calls in a Javascript Class with Callbacks

开发者 https://www.devze.com 2023-02-14 15:01 出处:网络
I\'m using JavaScript, jQuery, and JSONP to make asynchronous, cross-domain WCF service calls.I\'ve gotten this functionality successfully working, so I know that the problem I\'m experiencing is not

I'm using JavaScript, jQuery, and JSONP to make asynchronous, cross-domain WCF service calls. I've gotten this functionality successfully working, so I know that the problem I'm experiencing is not on the service side.

I had my test client site set up to make inline jQuery calls using the getJSON method, and it was working fine. However, I then tried to take the jQuery calls and put them into a JavaScript class. Now I'm having trouble getting callbacks to fire.

This works fine (the functions in the working example are added in script tags on the page itself):

function handleResponse(result) {
    if (result.Success) {
        // do something
    }
    else {
        // do something else
    }
}

function validate(serviceURL, data) {
    $.getJSON(serviceURL + "/Validate?data=" + data开发者_Go百科 + "&callbackHandler=?", handleResponse);
}

When I attempted to create a class to wrap this functionality in a separate .js include file, the callbacks don't fire. Here is the class code from the .js include file:

function serviceProxy(myServiceURL) {
    this.serviceURL = myServiceURL;

    this.validate = function(data, successCallback, failureCallback) {
        $.getJSON(this.serviceURL + "/Validate?data=" + data + "&callbackHandler=?", function(result) {
            if (result.Success) {
                successCallback();
            }
            else {
                failureCallback(result.ErrorMessage);
            }
        });
    };

And here is the JavaScript that's written in script tags directly on the page to make the calls:

function handleSuccess() {
    // do something
}

function handleFailure(message) {
    // do something else
}

function validate(serviceURL, data) {
    var proxy = new serviceProxy(serviceURL);
    proxy.validate(data, handleSuccess, handleFailure);
}

When debugging, the getJSON call is executed, but the handleSuccess and handleFailure callbacks are never reached. Any advice would be greatly appreciated.

Thanks in advance,

Ben


Have you tried replacing:

if (result.Success) {
            successCallback();
    }

with:

if (result.Success) {
            successCallback(result);
    }
0

精彩评论

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

关注公众号