Ashutosh Parashar
Ashutosh Parashar

Reputation: 85

Getting undefined result on ajax success

I am trying to call my controller by jquery ajax.My controller action method.My ajax is runnig successfully and my method is being called but it responses with a undefined result.Can anyone help me out with this My Ajax Code is

$.ajax({
  url: url,
  data: data,
  type: "POST",
  contentType: "application/json;charset=utf-8",
  dataType: "json",
  async: false,
  success: function(res) {
    alert(res.d);
  },
  error: function(res) {
    alert(res.status);
  }
})

it calls to a student controller which have a action method named create which is defined as below [HttpPost]

public JsonResult Create(string name)
    {
        return Json(name,JsonRequestBehavior.AllowGet);
    }

Upvotes: 1

Views: 4175

Answers (1)

Dilip Oganiya
Dilip Oganiya

Reputation: 1554

Please write your method as this way. Now you are returning in success fucntion only res.d object (but res.d not exists), rather than you should write res.name because you are returning name from Json method.

$.ajax({
  url: url,
  data: data,
  type: "POST",
  contentType: "application/json;charset=utf-8",
  dataType: "json",
  async: false,
  success: function(res) {
    alert(res.name);
  },
  error: function(res) {
    alert(res.status);
  }
})

Upvotes: 1

Related Questions