Ajax Receive String List from a Webmethod

Asked

Viewed 719 times

2

I need to pass a string list of a server method to ajax, but it doesn’t work. If I just pass a string it works.

Follows the code:

    [System.Web.Services.WebMethod]
    public List<string> MontarGrafico()
    {
          var l = new List<string>();
          l.Add("teste1");
          l.Add("teste2");
          return l;
    }

And in ajax:

    $.ajax({
        type: "POST",
        url: "Grafico.aspx/MontarGrafico",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (produtos) {
            var prods = produtos.d;
            $.each(prods, function (index, prod) {
                alert(prod);
            });                
        }
    });

But he exhibits nothing.

1 answer

2


You must be making that mistake:

inserir a descrição da imagem aqui

That is, failure to authenticate, to get around such problem goes in the folder App_Start, open the file RouteConfig.cs and left so, being that in the second line put as RedirectMode.Off

public static class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
       var settings = new FriendlyUrlSettings();
       settings.AutoRedirectMode = RedirectMode.Off;
       routes.EnableFriendlyUrls(settings);
    }
}

Ready will work immediately. inserir a descrição da imagem aqui


A tip: make a WebService.asmx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebApplication2
{   

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)] 
    [System.Web.Script.Services.ScriptService]
    public class WebServiceDados : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod()]
        public List<string> MontarGrafico()
        {
            var l = new List<string>();
            l.Add("teste1");
            l.Add("teste2");
            return l;
        }
    }
}

Javascript Ajax:

$.ajax({
        type: "POST",
        url: "WebServiceDados.asmx/MontarGrafico",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (produtos) {
            var prods = produtos.d;
            $.each(prods, function (index, prod) {
                alert(prod);
            });                
        }
});

In this mode you don’t need to touch that setting RouteConfig.cs

Another point to note is the sending and receiving, configure your jsonSerialization on your Webconfig, to receive/send data with large sizes

<configuration> 
   <system.web.extensions>
       <scripting>
           <webServices>
               <jsonSerialization maxJsonLength="50000000"/>
           </webServices>
       </scripting>
   </system.web.extensions>
</configuration> 

Note: to check the errors I install the Firebug Lite, a widely used plugin that picks errors Javascript by browser.

  • 1

    I added Webservice.asmx, and it worked. How do I view that part of the Post? The print you put up?

  • I use a plugin in google Chrome Firebug Lite: https://getfirebug.com/firebuglite

Browser other questions tagged

You are not signed in. Login or sign up in order to post.