I can not consume a Web service with jQuery; Answer Requested page not found [404]

4

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 captured the errors in the alert and I get: "Requested page not found [404]" .

What could be the reason why this webservice is not being used correctly?

    
asked by Xavier 24.12.2015 в 11:40
source

4 answers

2

The problem was that he returned a list directly, instead of returning a json and the ajax function did not recognize it. Here I leave the solution:

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 });

    JavaScriptSerializer jss = new JavaScriptSerializer();

        string resultat_Json = jss.Serialize(Listclient);

        return resultat_Json;
}
    
answered by 07.01.2016 / 15:43
source
4

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

$("#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 verify the errors better, the alert is not very good for that, instead in failure as in error use the output of the browser to verify the data in greater detail, it is more placed there a breakpoint from the browser and examine the response object to get as much information as possible about the error.

console.log(response.);
    
answered by 04.01.2016 в 19:00
1

Xavier, first of all confirm that you have added the Jquery file in your html.

 <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>

Then, from what I see, you're working with C #, and when you add a webServices file, it creates you the default code.

/// <summary>
/// Descripción breve de WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// Para permitir que se llame a este servicio Web desde un script, usando ASP.NET AJAX, quite la marca de comentario de la línea siguiente. 
//[System.Web.Script.Services.ScriptService]

If you look at the last line of the ScriptService, commented by default for the operation of this through a call from JS, it is necessary to remove the comment. In this way, we indicate that the class contains services that will be accessed from the client using scripting (javascript).

Then finally it should look like this:

/// <summary>
/// Descripción breve de WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// Para permitir que se llame a este servicio Web desde un script, usando ASP.NET AJAX, quite la marca de comentario de la línea siguiente. 
[System.Web.Script.Services.ScriptService]

Try it, because the ajax call is correct.

    
answered by 24.12.2015 в 14:21
1

The error that is showing you is that you can not find the GetCliente method. If you have the ClientServei.asmx/GetClient within a project folder (in another directory level) then try placing it like this:

url: "/carpeta/ClientServei.asmx/GetClient"
    
answered by 05.01.2016 в 07:45