Pinki Bansal
Pinki Bansal

Reputation: 63

ajax success returning undefined data object

I am returning a view from my action method which is called by ajax call. but in ajax success it is returning undefined object. What is the Problem?

$.ajax({
          type: "GET",
          url: url,
          success: function (data) {
              if (typeof (data) === 'undefined') {
                  alert("Error");
                  return;
              }else {
                  $('#content').html(data);
              }
          },
          error: function () {
              alert("Error");
              return;
          }
});

Backend code is here :-

public ActionResult Index()
{
     return view(); //Index is a view containing only "hello world"
}

Upvotes: 0

Views: 776

Answers (2)

Dilip Langhanoja
Dilip Langhanoja

Reputation: 4525

I am also using asp.net mvc and jquery ajax request in my application as well also tried your ajax code which is perfectly right. Possible reason for your error could be in your return view() like it could not be find.

One most another thing is as you are using ajax request it will not show any error directly for that you have to inspect in your browser console. I hope you will find problem while you inspect in your browser.

Upvotes: 0

Sombir Kumar
Sombir Kumar

Reputation: 1881

I think you declare a same variable in your code somewhere named data and assigned it undefined or you assigned "data = undefined" in console window.

If you do this either close the browser tab or browser itself. And try again it should work.

For more detail refer following links :-

http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

and

http://code.tutsplus.com/tutorials/javascript-hoisting-explained--net-15092

Upvotes: 1

Related Questions