3

我不知道我错过了什么,是否有需要更改的 IIS 设置,可能吗?

$(document).ready(function () {

    function AjaxSuccess(result) {

        alert('result: ' + result);
    }

    function AjaxError(result) {
        alert("error");
        alert(result.status + ' ' + result.statusText);
    }


    $(".hlp").click(function () {
        var myVal= $(this).val();
        var id = $(this).attr('id');


        $.ajax({
            type: "POST",
            url: "AjaxWebMethods.aspx/GetHelpText",
            contentType: "application/json; charset=utf-8",
            data: "{'helpText' : '" + id + "'}",
            dataType: "json",
            success: AjaxSuccess,
            error: AjaxError
        });
    });

});

我的网络方法如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class AjaxWebMethods : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e){ }

    #region Exposed WebMethods
    [System.Web.Services.WebMethod()]
    public string GetHelpText(string helpItem)
    {
        string helpText = "testing web method";
        return helpText;
    }
    #endregion
}

我不断收到 2 个弹出窗口“错误”,然后是 501 错误。请帮我解决这个问题。

4

2 回答 2

4

您需要将此行更改 data: "{'id' : '" + id + "'}",data: "{'helpItem' : '" + id + "'}",

因为 webmethod 将helpItem作为参数名称。

所以ajax函数最终会是

$.ajax({
            type: "POST",
            url: "AjaxWebMethods.aspx/GetHelpText",
            contentType: "application/json; charset=utf-8",
            data: "{'helpItem' : '" + id + "'}",
            dataType: "json",
            success: AjaxSuccess,
            error: AjaxError
        });

并使您的服务器端方法像这样静态

[System.Web.Services.WebMethod()]
    public static string GetHelpText(string helpItem)
    {

检查文章帮助你:从客户端脚本调用服务器端函数

于 2013-03-13T14:09:26.600 回答
3

您的数据是id并且您的 Web 方法是预期的helpItem。尝试对齐两者。

data: "{helpItem: '" + id + "'}"

要尝试的另一件事是添加static到您的 WebMethod 声明中。

[System.Web.Services.WebMethod()]
public static string GetHelpText(int helpItem)
{
于 2013-03-13T14:09:39.990 回答