简体   繁体   English

ajax成功返回未定义的数据对象

[英]ajax success returning undefined data object

I am returning a view from my action method which is called by ajax call. 我从ajax调用所调用的操作方法返回一个视图。 but in ajax success it is returning undefined object. 但是在ajax成功中,它返回的是未定义的对象。 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"
}

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. 我认为您在代码中的某个地方声明了一个相同的变量,名称为data,并且未定义它,或者在控制台窗口中分配了“ data = undefined”。

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 http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

and

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

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. 我也在我的应用程序中使用asp.net mvc和jquery ajax请求,还尝试了完全正确的ajax代码。 Possible reason for your error could be in your return view() like it could not be find. 错误的可能原因可能是在您的return view()中,找不到它。

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. 另一件事是,当您使用ajax请求时,它不会直接显示任何错误,因为您必须在浏览器控制台中进行检查。 I hope you will find problem while you inspect in your browser. 希望您在浏览器中检查时发现问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM