[英]Ajax call keeps giving me “internal server error 500”
我对ajax不太满意,但是如果存储过程不返回数据或返回数据,并且如果正在返回数据,则尝试在背后的代码中调用cehck方法,然后使该方法返回一个布尔值,以真正。 我将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 };
}
taskID是后面代码中的int
,而在JavaScript中是Array
。
data: "{'taskID':['" + selectedTasksList.join(',') + "']}",
将需要一个方法签名,例如:
public static bool[] HasExhibitLinked(List<int> taskID)
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) { } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.