为什么会出现此错误:"Uncaught TypeError:无法读取未定义的属性'Title'"? [英] Why am i getting this error: "Uncaught TypeError: Cannot read property 'Title' of undefined"?
问题描述
我正在尝试编写一个 ajax 网络应用.我有一个函数,该函数应该请求一个 json 对象,然后使用它来重新/填充网站.
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.
这是问题(第8至16行)中的 JavaScript :
window.onload=LoadData("Home", {});
var _doc = {};
function LoadData(page, params) {
$.get(page, params, function ( data ) {
_doc = jQuery.parseJSON( data );
}
);
document.title = _doc.Title.Title;
};
这是 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
这里证明它不是未定义的:
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
推荐答案
您只能在回调中通过AJAX请求访问data
:
You can only access the data
from the AJAX request in the callback:
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请求(异步 JavaScript和XML)请求是异步的;浏览器会发起请求,并且不等待响应...而是继续执行JavaScript. 稍后一段时间,当针对AJAX请求的HTTP请求完成时,您为AJAX请求提供的回调将被调用,并且可以访问HTTP响应中包含的数据.
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.
根据您的情况,document.title = _doc.Title.Title;
是在分派AJAX请求之后(即在上述稍后一段时间发生之前)立即执行;因此回调_doc = jQuery.parseJSON( data );
尚未触发,因此_doc
仍然是一个空对象,因此_doc.Title
是未定义的,并且尝试在未定义的_doc.Title
上检索Title
会引发错误.
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.
与您的问题无关,但仅供参考,您可能需要查看 jQuery.getJSON
方法; jQuery.get
方法的不同之处在于,您传递的响应对象已经 是JSON对象(因此您无需调用jQuery.parseJSON
).
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
).
这篇关于为什么会出现此错误:"Uncaught TypeError:无法读取未定义的属性'Title'"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!