简体   繁体   English

为什么我收到此错误:“未捕获的TypeError:无法读取未定义的属性'标题'”?

[英]Why am i getting this error: “Uncaught TypeError: Cannot read property 'Title' of undefined”?

I am trying to write an ajax web app. 我正在尝试编写一个ajax Web应用程序。 I have a function that is suppose to request a json object and then use it to re/populate the website. 我有一个函数,假设请求一个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: 并截图作为概述: 这就是Chrome控制台的样子...... Note: the call to the function at the bottom did change the title 注意:对底部函数的调用确实改变了标题

You can only access the data from the AJAX request in the callback: 您只能在回调中访问AJAX请求中的data

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; AJAX请求( 异步 JavaScript和XML)请求是异步的; the browser initiates the request, and does not wait for a response... instead the JavaScript execution is continued. 浏览器启动请求,而不是等待响应...而是继续执行JavaScript。 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请求的HTTP请求完成时,将调用您为AJAX请求提供的回调,并且可以访问HTTP响应中包含的数据。

In your situation, document.title = _doc.Title.Title; 在你的情况下, document.title = _doc.Title.Title; is executed immediately after the AJAX request is dispatched (ie before the some time later mentioned above has occured); 在调度AJAX请求之后立即执行(即在上面提到的一段时间之后发生); so the callback _doc = jQuery.parseJSON( data ); 所以回调_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. 还没有解雇,所以_doc仍然是一个空对象,所以_doc.Title是未定义的,并且尝试在未定义的_doc.Title上检索Title _doc.Title错误。

Unrelated to your problem, but FYI, you might want to look at the jQuery.getJSON method; 与您的问题无关,但仅供参考,您可能需要查看jQuery.getJSON方法; 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.get方法之间的区别在于您传递的响应对象已经是JSON对象(因此您不需要调用jQuery.parseJSON )。

Your $.get() function is asynchronous. 你的$.get()函数是异步的。 That means that when you call it, all you are doing is STARTING the execution of the ajax call. 这意味着当你调用它时,你所做的就是开始执行ajax调用。 Thus, the line following it: 因此,它之后的行:

document.title = _doc.Title.Title;

is executing BEFORE the ajax call has completed. 在ajax调用完成之前执行。 You only know the ajax call has completed in it's completion function. 您只知道ajax调用已完成其完成功能。

To fix this, put the reference to _doc in the ajax completion function: 要解决此问题,请在_doc中添加对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;
        }
    );
};

暂无
暂无

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

相关问题 我为什么会收到错误:Uncaught TypeError:无法读取未定义的属性“ left” - Why am I getting the error: Uncaught TypeError: Cannot read property 'left' of undefined 为什么我收到此错误:未捕获的TypeError:无法读取undefined的属性'john' - Why am I getting this error: Uncaught TypeError: cannot read property 'john' of undefined 为什么我会收到Uncaught TypeError:无法在分配时读取未定义的属性“ 0”? - why am I getting Uncaught TypeError: Cannot read property '0' of undefined on assign? 为什么会出现Uncaught TypeError:无法读取未定义的属性'displayQuestion'? - Why am i getting Uncaught TypeError: Cannot read property 'displayQuestion' of undefined? 在 Firebase 中,我收到 Uncaught TypeError: Cannot read property 'initializeApp' of undefined,但不确定为什么未定义“firebase”? - In Firebase I am getting an Uncaught TypeError: Cannot read property 'initializeApp' of undefined, but not sure why 'firebase' is not defined? 为什么我得到 Uncaught (in promise) TypeError: Cannot read property 'type' of undefined? - Why am I getting Uncaught (in promise) TypeError: Cannot read property 'type' of undefined? 为什么我收到“Select.js:1555 Uncaught TypeError:无法读取未定义的属性‘isSelectOptGroup’” - Why I am getting "Select.js:1555 Uncaught TypeError: Cannot read property 'isSelectOptGroup' of undefined" 为什么我会收到“未捕获的类型错误:无法读取未定义的属性 'body'”? - Why am I getting "Uncaught TypeError: Cannot read property 'body' of undefined"? 我收到错误,因为 Uncaught TypeError: Cannot read property 'use' of undefined - I am getting error as Uncaught TypeError: Cannot read property 'use' of undefined 运行此程序后出现错误,Uncaught TypeError:无法读取未定义的属性“ push” - I am getting error after run this program, Uncaught TypeError: Cannot read property 'push' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM