I am trying to write an ajax web app. I have a function that is suppose to request a json object and then use it to re/populate the website.

我正在尝试编写一个ajax web应用程序。我有一个函数,假设请求一个json对象,然后使用它来重新/填充网站。

Here is the Javascript in Question (Lines 8 - 16):

这是问题中的Javascript(第8 - 16行):

window.onload=LoadData("Home", {});
var _doc = {};
function LoadData(page, params) {
    $.get(page, params, function ( data ) { 
        _doc = jQuery.parseJSON( data ); 
        }
    );
    document.title = _doc.Title.Title;
};

Here is the error Chrome gave:

这是Chrome给出的错误:

Uncaught TypeError: Cannot read property 'Title' of undefined
LoadDatahttp://127.0.0.1/:15
(anonymous function)

This is what has me confused if I run the same statement in the console:

如果我在控制台中运行相同的语句,这让我感到困惑:

document.title = _doc.Title.Title;
"Home"

And it changes the title to Home

它将标题更改为Home

Here is proof that its not undefined:

这是证明其未定义:

_doc
Object
   Body: Object
      Menus: Array[4]
         0: Object
            Menu: Object
         1: Object
            Menu: Object
         2: Object
            Menu: Object
         3: Object
            Menu: Object
   Title: Object
      Title: "Home"
   User: Object
      Name: "Username"

And A screenshot as an overview: Note: the call to the function at the bottom did change the title

并且截图作为概述:注意:对底部函数的调用确实改变了标题

2 个解决方案

#1


6

You can only access the data from the AJAX request in the callback:

您只能在回调中访问AJAX请求中的数据:

window.onload=LoadData("Home", {});
var _doc = {};
function LoadData(page, params) {
    $.get(page, params, function ( data ) { 
          _doc = jQuery.parseJSON( data ); 
          document.title = _doc.Title.Title;
        }
    ));
};

AJAX requests (Asynchronous JavaScript and XML) requests are asynchronous; the browser initiates the request, and does not wait for a response... instead the JavaScript execution is continued. Some time later, when the HTTP request for the AJAX request has completed, the callback you provided for the AJAX request is invoked, and has access to the data contained in the HTTP response.

AJAX请求(异步JavaScript和XML)请求是异步的;浏览器启动请求,而不是等待响应...而是继续执行JavaScript。一段时间后,当AJAX请求的HTTP请求完成时,将调用您为AJAX请求提供的回调,并且可以访问HTTP响应中包含的数据。

In your situation, document.title = _doc.Title.Title; is executed immediately after the AJAX request is dispatched (i.e. before the some time later mentioned above has occured); so the callback _doc = jQuery.parseJSON( data ); has not fired yet, so _doc is still an empty object, so _doc.Title is undefined, and trying to retrieve Title on the undefined _doc.Title is throwing the error.

在你的情况下,document.title = _doc.Title.Title;在调度AJAX请求之后立即执行(即在上面提到的一段时间之后发生);所以回调_doc = jQuery.parseJSON(数据);尚未解雇,所以_doc仍然是一个空对象,所以_doc.Title是未定义的,并且尝试在未定义的_doc.Title上检索标题会引发错误。

Unrelated to your problem, but FYI, you might want to look at the jQuery.getJSON method; the difference between that that the jQuery.get method is that the response object you get passed will already be the JSON object (so you won't need to call jQuery.parseJSON).

与您的问题无关,但仅供参考,您可能需要查看jQuery.getJSON方法; jQuery.get方法之间的区别在于您传递的响应对象已经是JSON对象(因此您不需要调用jQuery.parseJSON)。

更多相关文章

  1. 在jQuery的$.post中调用函数时,Undefined不是对象
  2. 如何使函数等到对象的值未定义为js setTimeout
  3. 如何在javascript中合并2个对象[重复]
  4. JavaScript学习07 内置对象
  5. javascript的密封对象之seal(),isSealed()方法
  6. JavaScript String(字符串对象)
  7. IE:令人难以置信的跳跃表标题
  8. AngularJS(1.5.8) - 如何直接从获取json对象的控制器中填充选择选
  9. 使用append方法将对象转换为字符串

随机推荐

  1. printf在c语言中什么意思
  2. c语言中void的含义
  3. c语言的基本组成单位是什么
  4. c语言switch case语句怎么用
  5. c语言strcpy函数用法
  6. c语言scanf是啥意思
  7. scanf和getchar的区别
  8. c语言char是什么意思
  9. return后面的值不能为表达式吗?
  10. c语言define什么意思