Skip to content Skip to sidebar Skip to footer

.jquery .ajax How Do I Call Wcf Method With Return Value?

I have been able to call WCF methods with .ajax. How do I call a method that returns a value? I need to call this method to see if data is ready and if not wait one second. The WCF

Solution 1:

The return value will be contained in the data parameter passed to the success callback setup in your ajax call.

You will need to check the value here, then if false, setup a timeout, which on expiry will attempt the ajax call again.

It would be best IMO to wrap up the Ajax call inside a function, that you can call in a recursive fashion when the timeout has expired. e.g.

functionCallIsDataReady(input){
        $.ajax({
            url: "http://www.blah.com/services/TestsService.svc/IsDataReady",
            type: "GET",
            contentType: "application/json; charset=utf-8",
            data: input,
            dataType: "json",
            success: function(data) {
                if (!data){
                    setTimeout(function(){CallIsDataReady(input);}, 1000);
                }
                else{
                  //Continue as data is ready
                }
            }
        });
    }
    $(document).ready(function() {
        var input = {requestGUID:"<%=guid %>"};
        console.log(input);

        CallIsDataReady(input);
    });

Solution 2:

When you view source on this page is:

var input = {requestGUID:"<%=guid %>"};

showing correctly in the javascript? If you put a breakpoint in your IsDataReady method, do you see if requestGUID has a value? Is your service on the same domain as the page calling it?

EDIT: In your service change: [WebGet] to:

[WebGet(
RequestFormat=WebMessageFormat.Json,
ResponseFormat=WebMessageFormat.Json]

Read up on RESTful web services: http://www.codeproject.com/Articles/105273/Create-RESTful-WCF-Service-API-Step-By-Step-Guide

Post a Comment for ".jquery .ajax How Do I Call Wcf Method With Return Value?"