Home JQuery Ajax , why in success i have error why?
Reply: 1

JQuery Ajax , why in success i have error why?

mahmod nasser
1#
mahmod nasser Published in 2017-11-14 14:12:07Z

Why in success I have error?

$('#ck').click(function() {
  var StudentData = [{
    'Name': 'Ali',
    'Age': 27
  }, {
    'Name': 'Sam',
    'Age': 32
  }];

  $.ajax({
    url: "/Home/StudentInfo",
    data: JSON.stringify(StudentData),
    ...
    success: function(response) {
      alert(response[0].Name);
    },
    error: function() {
      alert("error");
    },
  });
});
David
2#
David Reply to 2017-11-14 14:51:18Z

Based on an excessively lengthy comment thread above, you claim to have this server-side code:

public JsonResult StudentInfo(List<object> StudentData)
{
    return Json(StudentData);
}

So, you're returning a List<object> to your client-side code. Then in your client-side code you try to access a property on an element in that list:

success: function (response) {
    alert(response[0].Name);
}

Well, to put is simply, object doesn't contain a property called Name. It never has, and it probably never will. So you can't access a property that doesn't exist. Hence, it's undefined in JavaScript.

It looks like you meant to define a class for your objects. Something as simple as this:

public class Student
{
    public string Name { get; set; }
    public int Age { get; set; }
}

Then you can use that class in your action method:

public JsonResult StudentInfo(List<Student> StudentData)
{
    return Json(StudentData);
}

Since Student does have a property called Name, you can use that property in your client-side code.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.438199 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO