c# – I can't consume a web service with jQuery; reply Requested page not found [404]

Question:

This is a fragment of the webservice:

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)],   
[System.ComponentModel.ToolboxItem(false)], 
[System.Web.Script.Services.ScriptService])
public class ClientServei : System.Web.Services.WebService
{
    [WebMethod]
    public List<Client> GetClient()
    {
        List<Client> Listclient = new List<Client>();
        Listclient.Add(new Client() { Nombre = "OSCAR", Apellido = "PUIG", Edad = 30 });
        Listclient.Add(new Client() { Nombre = "JORDI", Apellido = "FERRER", Edad = 31 });
        Listclient.Add(new Client() { Nombre = "MIQUEL", Apellido = "MAR", Edad = 31 });

        return Listclient;
    }
}

And this is the jQuery click function that I use to call the AJAX function:

$("#prova").click(function () {    
    $.ajax({
               url: "ClientServei.asmx/GetClient",
               data: "{}",
               dataType: "json",
               type: "POST",
               contentType: "application/json; charset=utf-8",
               success: function (data) {
               alert("success");
            },
            error: function (response) {
                alert("error");
            },
            failure: function (response) {
                 alert("arriva failure");
            }
      });
});

I have caught the errors in the alert and I get: "Requested page not found [404]" .

What could be the cause of this webservice not being consumed correctly?

Answer:

"Requested page not found [404]" is quite clear something is wrong in the URL or there is no access to it, try using the full url in the service call

$("#prova").click(function () {

            $.ajax({
                url: "http://www.myservice.com/ClientServei.asmx/GetClient",
                data: "{}",
                dataType: "json",
                type: "POST",
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert("success");
                },
                error: function (response) {
                    alert("error");
                },
                failure: function (response) {
                    alert("arriva failure");
                }
            });
    });

Make sure you have this / or similar according to the version / in the web.config .

<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>

You should also check errors better, the alert is not very good for that, instead both in failure and in error uses the browser's output to verify the data in greater detail, it is more place there a breakpoint from the browser and examine the response object to get as much information as possible regarding the error.

console.log(response.);
Scroll to Top