ozsenegal
ozsenegal

Reputation: 4133

Call Web Service .asmx using $.get jquery

I need to call a simple method from a WfService.asmx file,using $.get().

It works fine with POST method.Here's the Method:

 [WebMethod]
 public int Sum()
 {
  return 10 + 10;
 }

Now code using Jquery:

$.get('WfService.asmx/Soma',function(data){
alert(data.d);},"json");

And i get a error message. What am i doing wrong?

Upvotes: 0

Views: 1977

Answers (3)

Motoo
Motoo

Reputation: 90

example posted by @Aaronaught works, but with just one note=> you need to add attribute to your web service method:

[ScriptMethod(UseHttpGet = true,ResponseFormat=ResponseFormat.Json)]

example:

 [WebMethod(EnableSession = true)]
[ScriptMethod(UseHttpGet = true,ResponseFormat=ResponseFormat.Json)]
public int Add(int num1,int num2) {


    return num1 + num2;
}

And in your JQUERY ajax call you need to change type from POST to GET.I work with .NET 4 and i also don't need this configuration in web.config file..

Cheers

Upvotes: 0

Aaronaught
Aaronaught

Reputation: 122654

To get an ASP.NET SOAP Web Service (asmx) working with jQuery, you first have to add a ScriptServiceAttribute (found in System.Web.Script.Services) to the service itself:

[ScriptService] // <--- here
public class MyWebService : WebService
{
    [WebMethod]
    public int Sum(int x, int y)
    {
        return x + y;
    }
}

If the project wasn't created as an ASP.NET AJAX project, you may need to modify your web.config with the following:

<configuration>
  <system.web>
    <httpHandlers>
      <remove verb="*" path="*.asmx"/>
      <add verb="*" path="*.asmx" validate="false"
           type="System.Web.Script.Services.ScriptHandlerFactory, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
    </httpHandlers>
  </system.web>
</configuration>

(Note - update the System.Web.Extensions assembly reference with the appropriate version - this is for ASP.NET AJAX 1.0, which is not current. I don't have the info for 2.0, otherwise I'd post it.)

The jQuery code can be written like so:

$.ajax({
    type: "POST",
    url: "/MyWebService.asmx/Sum",
    data: "{x: 2, y: 5}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(msg) {
        $("#Result").html(msg);
    }
});

Note that this will break any clients that rely on the XML service - I'm assuming for the sake of this answer that the only consumer of the web service will be web sites using jQuery or at least JavaScript. If you need to support both scenarios then you will need to create two web services.

Upvotes: 1

Nathan Taylor
Nathan Taylor

Reputation: 24606

In order to communicate with a SOAP web service you must send it a properly formatted SOAP XML that conforms to the WSDL defined by the service. Unless you've explicitly set your ASP.NET web service to return JSON data you will not be able to make a $.get() request without first creating a SOAP message.

Upvotes: 2

Related Questions