this is my JSON object in the controller, I need to access this object from the ajax success function,
$.ajax({
url: "@Url.Action("Temp","GRN")",
type: "POST",
data: { term: request.term },
dataType: "json",
success: function (data) {
alert(data.msg);
}
});
this is my json in controller
var Item = new[]
{
new { Item_Name = "Soap", Item_Code = "IT1", Purchase_Price = 10.00, Sell_Price = 20.50},
new { Item_Name = "Pen", Item_Code = "IT2", Purchase_Price = 20.00, Sell_Price = 30.00},
new { Item_Name = "Paper", Item_Code = "IT3", Purchase_Price = 30.00, Sell_Price = 40.00},
new { Item_Name = "Brush", Item_Code = "IT4", Purchase_Price = 40.00, Sell_Price = 50.00}
};
return Json(Item);
The JSON object is the data
being passed to the success
function.
You are alerting data.msg
which isn't defined, so you'll likely get undefined
in an alert.
Looks like an array of data, so you probably want to iterate the result:
success: function (data) {
$.each(data, function(i, item){
console.log(item, item.Item_Name); //etc...
});
}
data
is your json
.
data[0].Item_Name // "Soap"
data[0].Item_Code // "IT1"
data[1].Item_Name // "Pen"
You can iterate over your data array with a simple for:
for(var i = 0 ; i < data.length ; i++)
{
var curr = data[i];
curr.Item_Name; //
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.