LLamar Asp.net WebMethod using Jquery Ajax

0

I am calling an Asp.Net C # method since a script works perfectly for me when it is in the url: 'default.aspx / NombreDelMetodo' but when I enter the project where I must implement the code I have problems with the url try these three forms but give me error

  url: "@Url.Content('~/Delivery/mapGoogle.aspx/MiMetodo')"
  url: "/Delivery/mapGoogle.aspx/MiMetodo",
  url: "../Delivery/mapGoogle.aspx/MiMetodo",

se ejecuta en mi navegador http://localhost:55531/Delivery/mapGoogle.aspx

Micodigo

function InvocarMetodoWeb() {
     $.ajax({
           type: "POST",
           url: "@Url.Content('~/Delivery/mapGoogle.aspx/MetodoWeb')",
           data: '{name: "' + 'EFRAIN' + '" }',
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: OnSuccess,
           error: function (result) {
               alert("Error" + result);
           }
          });

         }
    function OnSuccess(response) {
                        alert(response.d);
           }

my file system

    
asked by Efrain Mejias C 05.09.2016 в 00:14
source

1 answer

1

I've done the same thing you're doing:

  • Use ASP.net MVC and add a FolderRandom where a webform is located.
  • I can call the function from the same webform: http://localhost:53633/carpetaRandom/Webform.aspx or from any other part of the system using AJAX .

HTML

<input type="text" name="nombre" id="nombre" placeholder="escribe tu nombre" value="" />    
<button type="button" class="btn btn-primary" onclick="ShowCurrentTime()">
     llamar al WebMethod 
</button>

JavaScript :

function ShowCurrentTime() {
        $.ajax({
            type: "POST",
            url: '@Url.Content("~/CarpetaRandom/WebForm.aspx/GetCurrentTime")',        
            data: '{name: "' + $("#nombre").val() + '" }',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function (response) {
                alert(response.d);
            }
        });
}

function OnSuccess(response) {
    alert(response.d);
}

In the WebForm.aspx.cs file

[System.Web.Services.WebMethod]
        public static string GetCurrentTime(string name)
        {
            return "Saludos " + name + Environment.NewLine + "La hora es: " + DateTime.Now.ToString();
        }

Resulting in:

    
answered by 06.09.2016 / 23:52
source