0

我对 ajax 不是很好,但是如果存储过程没有返回数据或返回数据,并且如果它正在返回数据,那么我试图在后面的代码中调用一个方法来 cehck,如果它正在返回数据,那么让该方法返回一个 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 };
}
4

1 回答 1

3

taskIDint在后面的代码和ArrayJavaScript 中是一个。

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) {
            }
        });
于 2012-10-11T18:04:58.797 回答