views:

109

answers:

2

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?

+2  A: 

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.

Nathan Taylor
Once i've included explicity JSON return in my ASP.NET Web Service.Is the call using JQUERY correct?
I know it's been a minute, but yes it should be.
Nathan Taylor
+1  A: 

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.

Aaronaught
did you read the code?Im talking about GET request,not POST.Post is sandbox.Problem here s:How to call a SOAP Web service using $.get,and additionally i dont want to pass any parameters.
@ozsenegal: Yes, I read the code. ASMX web services **do not support `GET` semantics.** You can do it with WCF, but not SOAP. What is the problem with using an Ajax `POST` instead? What do you mean "sandbox?" If you don't want to pass any parameters then just leave the `data` blank.
Aaronaught
Ok,i got it.It doenst work cause SOAP Web services dont support GET requests,fine.Thank you.