2

Internally webservices use soap to work over HTTP. But when we try to access a [WebMethod] of a web service, how things start working on the basis of URL with jquery ajax? Does SOAP still playing the role with jQuery ajax? If yes how? If not why not? You can use below example to keep thing simple.

Below is the code from asmx:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class MyService : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld()
    {          
        return "Hello World";
    }
}
5
  • also how soap headers work with jquery ajax Commented Apr 19, 2017 at 11:35
  • @AndriiLitvinov please don't use inline code to highlight random terms. Commented Apr 21, 2017 at 12:45
  • @CodeCaster, could you elaborate a bit more? Or maybe provide link to wiki? I thought it is easier to read when tech things are highlighted. Commented Apr 21, 2017 at 12:46
  • @Andrii see for example Inline Code Spans should not be used for emphasis, right?. :) Commented Apr 21, 2017 at 12:51
  • 1
    @CodeCaster, makes sense, thanks! Commented Apr 21, 2017 at 12:53

1 Answer 1

0

It is possible to call WebMethods with AJAX as the transport is HTTP. You can find many examples of it in the internet and on SO:

jQuery AJAX call to an ASP.NET WebMethod

Calling ASP.Net WebMethod using jQuery AJAX

SOAP is an envelope for the payload (with some additional features). It is up to you whether you want to use it in WebMethod or not.

Here is how you create a Hello World service in web application project:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
public class WebService1 : System.Web.Services.WebService
{
    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }
}

Here is how you can consume it with jQuery:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.js"></script>

<script>
    console.log($.ajax);
    $.ajax({
        type: "POST",
        url: "http://localhost:55501/WebService1.asmx/HelloWorld",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(response.d) {
            alert(response.d);
        }
    });
</script>

And response from server will be {d: "Hello World"} because jQuery will add Accept header "application/json".

Here is how you can consume it from console app:

static void Main(string[] args)
{
    var client = new HttpClient();
    var uri = new Uri("http://localhost:55501/WebService1.asmx/HelloWorld")

    // Get xml
    var response = client.PostAsync(uri, new StringContent("")).Result;
    Console.WriteLine(response.Content.ReadAsStringAsync().Result);

    Console.WriteLine();

    // Get Json
    var response1 = client.PostAsync(uri,
        new StringContent("", Encoding.UTF8, "application/json")).Result;
    Console.WriteLine(response1.Content.ReadAsStringAsync().Result);
}

Which will output:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">Hello World</string>

{"d":"Hello World"}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Andrii thanks for your answer, could you please explain this with an example. I know how to call webmethod with jquery ajax. I want to know how things(searlization of soap messages) works internally when w are consuming asmx services in an application (in web application or console applicationi) and how things get automatticaly changed when we calling a webmethod from ajax.
Nothings changes automatically. SOAP is communication protocol over HTTP. So it doesn't matter if the client is another application or browser.
@user7889160, I have updated my answer with example.
Thanks really helped me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.