Call Parameterized asmx Web Service using JQuery

2.1k Views Asked by At

I am trying to call a webservice on my local host which expects a parameter pkId and return the result. WebService runs perfect but when i call from JQuery, it does not return any data. I have tried almost all combinations to set parameter for the Web service (data) part but unable to get any result and the real pain is i don't get any error.

    $.ajax({
        type: "POST",
        url: "http://localhost/WSTest/Service.asmx/AuthorGetById",
        data: "{pkId :'" + pkId + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            return msg.d;
        }
    });
3

There are 3 best solutions below

0
On

This is not an answer but means to help you reach there.


Even if you have an error you wont get it since you have not handled error at all.
Also worth while to check if your msg has 'd' since its a ASP.NET 3.5+ feature.
Also, dont return msg.d try to alert it.

A sample will be

$.ajax({
    type: "POST",
    url: "http://localhost/WSTest/Service.asmx/AuthorGetById",
    data: "{pkId :'" + pkId + "'}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
     success: function (msg) {
         var datum = msg.hasOwnProperty("d")? msg.d : msg;
         alert("Success" + datum);
        }
    error:function (xhr, ajaxOptions, thrownError){
        alert("Error");
    }
});
0
On

Success is invoked as a callback from AJAX. It means that its result is returned to the AJAX jQuery function, not as a return from your function making the AJAX call. You would need to pass result.d off to another function that makes use of it. Have you placed a breakpoint inside your success function to see what value result holds?

For information on consuming web services using AJAX calls, check out:

using jquery to consume aspnet json web services/

0
On

I found the answer. Actullay i was passing the parameters correctly and noticing the response in firebug but was unable to catch it on success because... my web service method return void. I changed it to return string and boooom it work fine.