[英]I can see the object's field variable in the console but cannot access it
我可以在控制台中看到对象的字段变量,但是无法通过访问objectName.fieldVariableName
来访问它
var ob = jQuery.get("file.txt");
console.log("This is the resulting object");
console.log(ob);
console.log("This is the responseText");
console.log(ob.responseText);
当我尝试访问中的响应文本时,显示为未定义。 但是,当我打印整个对象时,我可以看到正确的响应文本。 如何访问responseText字段变量?
由于jQuery.get
是异步方法,因此您需要提供一个回调函数 ,一旦请求成功,该函数将被调用:
var ob = jQuery.get("file.txt", function(responseText)
{
console.log("This is a jqXHR object");
console.log(ob);
console.log("This is the responseText");
console.log(responseText);
});
jQuery的调用的响应数据作为第一个参数(回调函数responseText
在上面的代码)。
在控制台中确实看到responseText
的原因可能是由于在扩展对象详细信息时,请求已成功完成,并且已设置了reponseText
字段。
另外,请注意, ob
不是结果对象,而是jqXHR
对象。
这是因为您需要使用回调函数来处理get
请求的success
事件。
$.get("file.txt", function(data) {
console.log(data);
});
在控制台中执行该请求时,请求已完成,并且已填充对象。 但是,在您的代码段中,请求在执行时尚未完成。
有关更多信息,请参见jQuery.get 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.