Ajax调用一直给我“内部服务器错误500”

时间:2012-10-11 18:03:22

标签: javascript jquery asp.net ajax stored-procedures

我对ajax不是很好,但是我试图在后面的代码中调用一个方法,如果存储过程没有返回任何数据或返回数据,并且如果它正在返回数据,那么请调用方法返回一个评估为true的bool。我正在传递一个id的列表到该方法。 但是我的ajax调用可能是错误的。

这是我的ajax:

var hasExhibitLinked = false;
            var selectedTasksList = getSelectedTaskIDs();

            $.ajax({
                type: "POST",
                url: '<%=ResolveUrl("~/Tasks/ViewTasks.aspx/HasExhibitLinked")%>',
                data: "{'taskID':['" + selectedTasksList.join(',') + "']}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {

                    arr = data.d;

                    hasExhibitLinked = arr[0];
                },
                error: function (data) {
                }
            });

            if (hasExhibitLinked) {
                showMessage("There is an Exhibit linked.");

            }
            else {
                showMessage("Not exhibits linked");

            }

如果需要更多信息,这里是我的代码: 编辑:

[WebMethod]
public static bool[] HasExhibitLinked(String[] taskID)
{
    bool hasLink = false;
    var conn = new SqlConnection(ConfigurationManager.ConnectionStrings["OSCIDConnectionString"].ToString());
    var cmd = new SqlCommand("p_Link_List", conn);
        cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.Add(new SqlParameter("@Taskid", SqlDbType.Int));

    foreach (var id in taskID)
    {
        cmd.Parameters["@Taskid"].Value = taskID;
        try
        {
            conn.Open();
            String s = (String)cmd.ExecuteScalar();
            if (s != null)
                hasLink = true;
        }
        catch (SqlException sql)
        {
            ErrorLogger.Log(sql.Number, sql.Source, sql.Message);
        }
        catch (Exception ex)
        {
            ErrorLogger.Log(ex);
        }
        finally
        {
            if (conn.State == ConnectionState.Open)
                conn.Close();
        }
    }
    return new bool[] { hasLink };
}

1 个答案:

答案 0 :(得分:3)

taskID是后面代码中的int和JavaScript中的Array

data: "{'taskID':['" + selectedTasksList.join(',') + "']}",

需要方法签名,如:

public static bool[] HasExhibitLinked(List<int> taskID)

<小时/> 问题二:评论:这是ajax常见的错误。该代码是异步的。因此,当您尝试访问该变量时,该变量不存在。这就是你看到很多“回调”功能的原因。

        var hasExhibitLinked = false;
        var selectedTasksList = getSelectedTaskIDs();

        $.ajax({
            type: "POST",
            url: '<%=ResolveUrl("~/Tasks/ViewTasks.aspx/HasExhibitLinked")%>',
            data: "{'taskID':['" + selectedTasksList.join(',') + "']}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {

                arr = data.d;

                hasExhibitLinked = arr[0];
                // Now it exists and is ready
                if (hasExhibitLinked) {
                    showMessage("There is an Exhibit linked.");
                } else {
                    showMessage("Not exhibits linked");
                }
            },
            error: function (data) {
            }
        });