I am working on a basic ASP.NET & AJAX/JSON assignment and I am frustrated with making it work. One of the requirements was to create an AJAX/JSON method to do some manipulations on SQL DB data.
In order to complete that, I used the following code:
In the aspx:
$.ajax({
type: "GET",
dataType: "json",
url: "Retrieve",
success: function (data) {
alert(data);
var col;
for (col in data) {
alert(col);
addRow(data[col].id, data[col].Name, data[col].catagory, data[col].difficulty, data[col].company, data[col].price, '#products');
}
},
error: function () {
alert("1");
}
In the "retrieve.aspx" page (the part of code that creates the data for JSON):
Response.ContentType = "application/json; charset=utf-8";
bool val1 = (System.Web.HttpContext.Current.User != null) && System.Web.HttpContext.Current.User.Identity.IsAuthenticated;
string category = Request.QueryString["category"];
//string a = JsonConvert.SerializeObject(getProducts(category));
var a = getProducts(category);
// instantiate a serializer
JavaScriptSerializer TheSerializer = new JavaScriptSerializer();
var TheJson = TheSerializer.Serialize(a);
Response.Write(TheJson);
And last but not least, if i try to change the dataType to text, it will show that the content is the JSON-structured text together with an HTML page code.
What am I missing here?