0

I'm trying to get bool from a webmethod, but got xml string instead. Don't want to parse XML on client. In .net MVC i've achieved it simply by returnin' JSON, how to make the trick via WebForms?

The webmethod:

[System.Web.Services.WebMethod]
    public bool MyService(string _container)
    {
       return true;
    }
    

The AJAX call:

$.ajax({
        type: "POST",
        url: '/WebMethods.asmx/MyService',
        contenttype: "application/json; charset=utf-8",
        data: { _container: JSON.stringify(params) },
        datatype: 'json',
        success: function (data) {
           if(data) {
              MakeStuff();
           }
        }
    });

What i've got: <boolean xmlns="%myWebmethodUrl%">true</boolean>

Dunadan
  • 23
  • 1
  • 7
  • Dupe http://stackoverflow.com/questions/18244696/how-to-return-json-with-asp-net-jquery ? – Vladimirs Nov 21 '14 at 10:14
  • 1
    @Vladimirs unfortunately, no. [The solution](http://stackoverflow.com/questions/18244696/how-to-return-json-with-asp-net-jquery) just changed type of XML from boolean to string. Not the way out. – Dunadan Nov 21 '14 at 10:30

1 Answers1

0

You may return response directly, like below

public void MyService(string _container)
{
   Context.Response.Write(true);
}


$.ajax({
        type: "POST",
        url: '/WebMethods.asmx/MyService',
        contenttype: "application/json; charset=utf-8",
        data: { _container: JSON.stringify(params) },
        datatype: 'json',
        success: function (data) {
        var BoolData = (data=="True") ? true : false;
           if(BoolData) {
              MakeStuff();
           }
        }
    });
Abbas Galiyakotwala
  • 2,949
  • 4
  • 19
  • 34