Jquery ajax get method with parameter is not working in asp.net

浪尽此生 提交于 2019-12-31 05:46:07

问题


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..


回答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




回答2:


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




回答3:


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)



来源:https://stackoverflow.com/questions/21382917/jquery-ajax-get-method-with-parameter-is-not-working-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!