我有一个奇怪的问题,而调用ajax jquery在asp.net..我得到了parseError,这是意料之外的,因为一切都在适当的地方。
下面是我的webmethod。
public class MyLogic
{
private int _id;
public int Id
{
get { return _id; }
set { _id = value; }
}
private string _title, _image;
public string Image
{
get { return _image; }
set { _image = value; }
}
public string Title
{
get { return _title; }
set { _title = value; }
}
}
下面的是我调用的
方法[WebMethod]
public static MyLogic[] GetTopArticles()
{
List<MyLogic> bList = new List<MyLogic>();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MobileKeyboardConnection"].ConnectionString);
SqlDataAdapter adapTopStories = new SqlDataAdapter("m_sp_toparticles", con);
adapTopStories.SelectCommand.CommandType = CommandType.StoredProcedure;
adapTopStories.SelectCommand.Parameters.AddWithValue("@PortalId", 2);
adapTopStories.SelectCommand.Parameters.AddWithValue("@topValue", 5);
DataTable dtTopStories = new DataTable();
adapTopStories.Fill(dtTopStories);
foreach (DataRow r in dtTopStories.Rows)
{
MyLogic c = new MyLogic();
c.Id = Convert.ToInt32(r["Id"]);
c.Title = r["Title"].ToString();
c.Image = r["image"].ToString();
bList.Add(c);
}
return bList.ToArray();
}
和下面是设计。
<script type="text/javascript">
$(document).ready(function () {
$.ajax({
type: "POST",
url: "AjaxLogic.aspx/GetTopArticles",
dataType: "json",
contentType: "application/json; charset=utf-8",
data: "{}",
success: function (data) {
var result = data.d;
alert(result.length);
},
error: function (data) {
alert(data.responseText);
}
});
});
</script>
请知道可能会有什么问题我在我的应用程序中使用核心asp.net和母版页。
* * * * * * * * * * * * * * * * * * JSON响应 * * * * * * * * * * * * * * * * *
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
</title></head>
<body>
<form name="form1" method="post" action="AjaxLogic.aspx" id="form1">
<div>
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZPKFQelTZBrnZbMRGP+4imyXfwO4" />
</div>
<div>
</div>
</form>
</body>
</html>
尝试替换:
data: {},
由:data: '{}',