【问题标题】:Jquery ajax get method with parameter is not working in asp.net带有参数的Jquery ajax get方法在asp.net中不起作用
【发布时间】:2014-02-18 09:54:45
【问题描述】:

我想使用 jquery ajax get 方法调用 Web 方法。但它没有被调用。在我的 javascript 和代码下方

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!");
        }
    });
}

我的函数背后的代码

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

有人可以帮我解决这个问题吗..

【问题讨论】:

标签: jquery asp.net get-request


【解决方案1】:

出于安全原因,ASP.Net AJAX 页面方法仅支持 POST 请求。

下面的例子显示使用POST请求

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# 页面方法

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

请记住使用 pagemethod 参数中使用的 ajax 发布数据变量名称。因为它是区分大小写的

【讨论】:

  • 我认为你错了。我已经发送了不带参数的 Get 请求,但我认为不允许带参数。
【解决方案2】:

在你的代码中试试这个:

$.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);
                    }
                });

为了确保您的代码,请始终使用console.log();

【讨论】:

    【解决方案3】:

    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 用于管理 ASP.NET 中 AJAX 功能的 HTTP 模块。有时 asp.net 加载不正确。您必须在 web.config 中手动包含 ScriptModule。

    参考:

    WebMethod not working. (language:Chinese)

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多