Javascript 为什么没有从AJAX调用我的C方法?
我正在尝试调用以下C方法:Javascript 为什么没有从AJAX调用我的C方法?,javascript,c#,sql,ajax,json,Javascript,C#,Sql,Ajax,Json,我正在尝试调用以下C方法: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static string getJSONdata() { string jsonString = ""; using (SqlConnection con = new SqlConnection(connectionString)) {
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string getJSONdata()
{
string jsonString = "";
using (SqlConnection con = new SqlConnection(connectionString))
{
con.Open();
using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 * FROM DRAW ORDER BY DrawID DESC;", con))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
List<Dot> _Dot = new List<Dot>();
while (reader.Read())
{
Dot dot = new Dot();
dot.x = (int.Parse(reader["xCoord"].ToString()));
dot.y = (int.Parse(reader["yCoord"].ToString()));
if (reader["DrawID"] != DBNull.Value)
dot.i = (int.Parse(reader["DrawID"].ToString()));
_Dot.Add(dot);
}
JavaScriptSerializer jss = new JavaScriptSerializer();
jsonString = jss.Serialize(_Dot);
}
}
}
System.Diagnostics.Debug.WriteLine(" JSON: " + jsonString);
return jsonString;
}
首先,我得到了ajax错误。不知道为什么不叫它。
其次,我仍然不确定是否返回了所需的正确JSON数据,无论格式是否正确。谢谢你的帮助
编辑。它正确地返回JSON字符串
注意:connectionString在另一个函数中工作,所以不是这样。下载并安装
这将允许您查看页面和服务器之间的通信量。您可以查看实际的调用URL,然后将其粘贴到浏览器中以查看其返回的内容。这将为您提供一个解决此类问题的工具 你得到了什么HTTP响应?@zerkms 500内部服务器错误使用调试器运行你的项目,看看有什么失败?@zerkms当我删除数据时,我不需要它,contentType我得到了一个200错误。特别是SyntaxError意外令牌
$.ajax({
url: 'Default.aspx/getJSONdata',
data: '{ }',
contentType: 'application/json; charset=utf-8',
dataType: 'json',
success: function (response) {
alert(response.d);
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});