0

I want to call a web method using jquery ajax get method.but it is not getting called. below my javascript and code

javascript:

    function RetrievePassword() {
    var forgotEmail = $("#txtForgotEmail").val().trim();

    //call the ajax method to retrieve the password from the email address provided.
    $.ajax({
        type: "GET",
        url: "test.aspx/RetrievePassword?email=" + forgotEmail,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            alert(result.d);
        },
        error: function () {
            alert("Error while calling the server!");
        }
    });
}

my code behind function

    [WebMethod]
    [ScriptMethod(UseHttpGet=true)]
    public static string RetrievePassword(string email)
    {
     //some code
}

Can anybody help me on this..

Wanna Coffee
  • 2,742
  • 7
  • 40
  • 66
sandeep.mishra
  • 825
  • 4
  • 19
  • 40

3 Answers3

1

For security reasons, ASP.Net AJAX page methods only support POST requests.

Below example is shown using POST request

jQuery

$.ajax({
    type: "POST",
    url: "test.aspx/RetrievePassword",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    data: '{email:"' + forgotEmail + '"}',
    success: function (result) {
        alert(result.d);
    },
    error: function () {
        alert("Error while calling the server!");
    }
});

C# Pagemethod

[WebMethod]        
public static void RetrievePassword(string email)
{
    //some code           
}

Remember to use ajax post data variable name as used in pagemethod's argument. As it's case-sesitive

Suraj Singh
  • 4,041
  • 1
  • 21
  • 36
Chirag Vidani
  • 2,519
  • 18
  • 26
0

try this into your code :

$.ajax({
                    url: 'URL',
                    data: "{ 'Keyword': '" + forgotEmail + "'}",
                    dataType: "json",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    success: function (data) {
                        response($.map(data.d, function (item) {
                            console.log(item);
                            return {
                                item;
                            }
                        }))
                    },
                    error: function (response) {
                        console.log(response.responseText);
                    },
                    failure: function (response) {
                        console.log(response.responseText);
                    }
                });

to be sure of your code, always go with console.log();

TheSM
  • 59
  • 1
  • 13
0

try this in the web.config:

<system.web>
 ...
 <httpModules>
    <add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
 </httpModules>
</system.web>

ScriptModule is for manageing HTTP modules for AJAX functionality in ASP.NET.,and sometimes asp.net is loading this improperly. You have to include ScriptModule manually in web.config.

Reference:

WebMethod not working. (language:Chinese)

劉鎮瑲
  • 517
  • 9
  • 20