Does not consume my web Service from Ajax Jquery

1

This is my code

$.ajax({
    type: "POST",
    url: 'http://localhost/QM/QMUI/WebServicePrueba.asmx/ObtenerCategorias',
    //url: "CategoriaK.aspx/ObtenerCategorias",//ojitos
    data: "{IdCategoria :" + -1 + " }",   // -1 trae la lista completa
    dataType: "json",
    async: false,
    cache: false,
    contentType: "application/json; charset=utf-8",
    success: function (datos) {

        //Valido si hubo Error, si es diferente a -1 es que hubo error
        var existeError = datos.d.indexOf('Error');
        if (existeError == -1) {
            registrosListado = JSON.parse(datos.d);
        }
        else {
            Error = datos.d;
            if (Error != "") {
                alert(Error);
                return false;
            }
        }
    }
});

This is my web service

#region <<ObtenerListadoCategorias>>
    [WebMethod(EnableSession = true)]
    public string ObtenerCategorias(Int16 IdCategoria)
    {
        string mensaje = "";
        DataTable DataTable = new DataTable();
        dbo.BaseDatos db = null;
        db = new dbo.BaseDatos(ConfigurationManager.ConnectionStrings["cnn"].ToString());
        SqlParameter prmIdCategoria;

        try
        {
            prmIdCategoria = new SqlParameter("@IdCategoria", SqlDbType.VarChar, 20);
            prmIdCategoria.Value = IdCategoria;


            DataTable = db.ObtenerDatosComoDataTable("spg_ListadoCategoria", prmIdCategoria);
            //dataset.TableName = "Usuario";
            mensaje = JsonConvert.SerializeObject(DataTable);
        }
        catch (Exception ex)
        {
            mensaje = ex.Message;
        }
        finally
        {
            db.Finalizar();
        }
        return mensaje;
    }
    #endregion

This is my Web service

Suggestions ??

    
asked by Erick Tovar 05.01.2017 в 19:28
source

2 answers

2

In your jQuery code you are declaring that you are going to consume a service of type type: "POST", however, as you can consume it from a browser the logic tells me that your service ASMX is by default type GET , change the following line in your ajax and perform the test:

$.ajax({
    type: "GET",
    url: 'http://localhost/QM/QMUI/WebServicePrueba.asmx/ObtenerCategorias',
    ....

Or in your case, declare in your service ASMX of type POST .

    
answered by 05.01.2017 в 20:40
1

You are missing the WSDL in the url:

url: 'http://localhost/QM/QMUI/WebServicePrueba.asmx/ObtenerCategorias?WSDL',
    
answered by 05.01.2017 в 20:16