I am having trouble reading JSON results in JavaScript as it keeps giving me an error. The error occurs specifically on var jsonObj = JSON.parse(msg);. The error being thrown is: Uncaught SyntaxError: Unexpected token o. Despite debugging the code, the Web service returns perfectly formatted JSON data; however, the error seems to be happening during the success event in the AJAX call. I would greatly appreciate any assistance on how to properly loop through this JSON object.
<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>
Webservice snippet:
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;
}