0

我想使用 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
}

任何人都可以帮我解决这个问题..

4

3 回答 3

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 发布数据变量名称。因为它是区分大小写的

于 2014-01-27T14:23:13.080 回答
0

在你的代码中试试这个:

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

于 2014-01-27T14:12:41.263 回答
0

试试这个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 加载不正确。您必须手动将 ScriptModule 包含在web.config.

参考:

网络方法不工作。(语言:中文)

于 2019-07-29T08:41:17.520 回答