javascript - asp.net 中的 500 内部服务器错误

标签 javascript c# jquery asp.net ajax

问题概述:我正在创建一个应用程序,其中有一个 html 选择列表,我可以从中选择一个类别,并使用 ajax webmethod 从该类别中获取项目和图像。

问题概述:我在 ajax linq to sql 中遇到了许多 500 错误并修复了它。但现在我正在开发 ado.net 应用程序,我面临的问题是当我从选择列表(catlist)中选择一个类别时,它从这里显示错误 error: function (xhr) {alert(xhr.status) );}.

  [ArgumentException]: Unknown web method elist.
 Parameter name: methodName
 at System.Web.Script.Services.WebServiceData.GetMethodData(String methodName)
 at System.Web.Handlers.ScriptModule.OnPostAcquireRequestState(Object sender, EventArgs eventArgs)
 at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
 at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
.

How can I fix this error. I thought I briefly describe my question.

Default.Aspx Code Overview:

Below is my HTML

<table>
    <tr>
        <td>
            <select id="catlist" runat="server" onchange="getImageUrl()"></select>
        </td>
        <td></td>
    </tr>
    <tr>
        <td>
            <img id="imgload" width="180" height="100" src="" alt="No Image Found" />
        </td>
        <td>
            <ul>
                <li>Description</li>
                <li>Loreum IspumLoreum IspumLoreum IspumLoreum IspumLoreum IspumLoreum IspumLoreum Ispum</li>
            </ul>
        </td>
    </tr>
</table>

下面是我的 Javascript 函数

function getImageUrl() {
        var catid = $("#catlist")[0].value;
        $.ajax({
            url: "Default.aspx/elist",
            data: { catId: catid },
            contentType: "Application/json; charset=utf-8",
            responseType: "json",
            method: "POST",
            success: function (response) {
                alert(response.d);
            },
            error: function (xhr) {
                alert(xhr.status);
            },
            Failure: function (response) {
                alert(response);
            }
        });
    }

Default.Aspx.cs 代码概述:

下面是我的自定义类

public class events
{
    public string EVE_NAME { get; set; }
    public string EVE_IMG_URL { get; set; }
    public string EVE_DESCRIPTION_SHORT { get; set; }
}

下面是Datatable方法

private static DataTable dt2(int catId)
{
    DataTable dataTable = new DataTable();
    SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=EVENT;Persist Security Info=True;User ID=sa;Password = 123");
        string query = "sp_view";

    SqlCommand cmd = new SqlCommand(query, conn);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add("@catID", SqlDbType.Int).Value = catId;
    conn.Open();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(dataTable);
    return dataTable;
 }

下面是WebMethod

[WebMethod]
private static List<events> elist(int catId)
{
     List<events> eve = new List<events>();
     eve = (from DataRow row in dt2(catId).Rows
           select new events
           {
                   EVE_NAME = row["EVE_NAME"].ToString(),
                   EVE_IMG_URL = row["EVE_IMG_URL"].ToString(),
                   EVE_DESCRIPTION_SHORT = row["EVE_DESCRIPTION_SHORT"].ToString(),
           }).ToList();
     return eve;
 }

注意: 数据库列名称和事件类属性名称相同。

最佳答案

根据属性WebMethod的文档:

https://msdn.microsoft.com/en-us/library/byxd99hx(v=vs.90).aspx

Attaching the WebMethod attribute to a Public method indicates that you want the method exposed as part of the XML Web service. You can also use the properties of this attribute to further configure the behavior of the XML Web service method. For more information, see Code Model for XML Web Services in Managed Code.

我想乔恩·斯基特是对的。 :)

关于javascript - asp.net 中的 500 内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35587680/

相关文章:

c# - 将 .NET 外部 dll 合并到单个可执行文件

c# - 在 C# 中使用 IPv6 连接到 mySql 服务器

javascript - 焦点文本字段检查它是否是数字

jquery - MVC ajax 返回字符串作为 [object] 对象

java - 背景随着淡入淡出而过渡/变化

javascript - Joi 验证 - 如何根据输入使字段可选

javascript - JS调用没有方法的对象时调用函数

javascript - react (单元和集成): Which framework to use for react unit testing and Integration testing

c# - 将方法作为参数传递

javascript - Sequelize - 在 PostgreSQL 数组查询中不包含字符串