Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling ASP.NET web service function via GET method with jQuery

I'm trying to call web service function via GET method using jQuery, but having a problem. This is a web service code:

[WebService(Namespace = "http://something.com/samples")]
[ScriptService]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TestWebService  : System.Web.Services.WebService {
    [WebMethod]
    public string Test2()
    {
        string result = null;

    try
        {
            result = "{'result':'success', 'datetime':'" + DateTime.Now.ToString() + "'";
        }
        catch (Exception ex)
        {
            result = "Something wrong happened";
        }

        return result;
    }

}

That's the way I call the function:

$.ajax({ type: "GET",
         url: "http://localhost/testwebsite/TestWebService.asmx/Test2",
         data: "{}",
         contentType: "application/json",
         dataType: "json",
         error: function (xhr, status, error) {
             alert(xhr.responseText);
         },
         success: function (msg) {
             alert('Call was successful!');
         }
     });

Method is called successfully, but result string gets covered by XML tags, like this:

<string>
{'result':'success', 'datetime':'4/26/2010 12:11:18 PM'
</string>

And I get an error because of this (error handler is called). Does anybody know what can be done about this?

like image 510
the_V Avatar asked Apr 26 '10 19:04

the_V


1 Answers

Enable ASP.NET ASMX web service for HTTP POST / GET requests

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public string Test2()
{
   [...]
}
like image 142
Spain Train Avatar answered Oct 13 '22 23:10

Spain Train