【发布时间】:2015-07-22 19:06:17
【问题描述】:
我正在尝试在 javascript 中读取 json 结果,但它给了我错误。
它在 var jsonObj = JSON.parse(msg); 上给了我错误。 错误:Uncaught SyntaxError: Unexpected token o 当我调试代码 Webservice 返回完美的 json 数据时,它在 ajax 中的成功事件上给出错误。 我如何遍历这个 json 对象?任何帮助表示赞赏。
<script type="text/javascript">
var ProductCategoryList;
function callpageload() {
$.ajax({
type: "GET",
url: "WebService1.asmx/GetCategoryList",
contentType: "application/json; charset=utf-8",
success: function (msg) {
var jsonObj = JSON.parse(msg);
},
error: function (msg) {
}
});
}
</script>
网络服务代码
public string GetCategoryList()
{
DataSet ds = Persistance.GetCategoryList();
List<ProductCategories> prodlst = new List<ProductCategories>();
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
ProductCategories prod = new ProductCategories();
prod.pid = ds.Tables[0].Rows[i]["pid"].ToString();
prod.id = ds.Tables[0].Rows[i]["id"].ToString();
prod.name = ds.Tables[0].Rows[i]["name"].ToString();
prodlst.Add(prod);
}
}
string json = JsonConvert.SerializeObject(prodlst.ToArray());
return json;
}
【问题讨论】:
-
什么错误?我没有看到任何错误。
-
为您的
ajax设置选项dataType: 'json'。我认为你不需要解析响应,因为它已经是 json 格式。
标签: javascript asp.net ajax json web-services