JSON 对象错误 Uncaught TypeError: 无法读取未定义的属性
我有一个问题,对于同一个对象,在警报窗口中给出值,并在浏览器中给出 JS 错误。
在“alert (obj.xtagName)”行给出错误消息。 不过,我也在警报对话框中看到了正确的输出。
对我来说很奇怪。
使用
浏览器:Chrome (v10.0.648.151)
错误 - 未捕获类型错误:无法读取未定义的属性“xtagName”
浏览器:IE (v8.0.7600.16385CO)
错误 - “obj.xtagName”为 null 或不是
有错误的对象对象- obj.xtagName
- 我有 PHP 页面(gXML.php),它生成 JSON 字符串。此 JSON 还使用“http://www.jsonlint.com/”进行验证。 使用 AJAX 调用,我获取此 JSON 并根据 responseText 将其转换为 JSON 对象。
- 文件开头:_ajax.js
AJAX 代码调用
function gQuote_getData() {
var request = _getHTTPObject();
var url = "php/gXML.php";
if (request) {
request.onreadystatechange = function() {gQuote_parseResponse(request);};
request.open( "GET", url, true );
request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1900 00:00:00 GMT");
request.send(null);
return true;
} else {
return false;
} }
XMLRespObj
function _getHTTPObject() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
xhr = false;
}
}
}
return xhr;}
接收响应 文件
function gQuote_parseResponse(request) {
var resp;
if (request.readyState == 4) {
if (request.status == 200 || request.status == 304) {
//Helps trim extra space at beginning and end of contruct
resp = (request.responseText.replace(/^\s+|\s+$/g, ''));
}
}
fillOutput (resp);
//printX(gQuote_JSON);}
- 结尾 js/_ajax.js
-
HTML 页面 JS 脚本
function fillOutput (resp_text) {
document.getElementById("output").value = resp_text
printX(resp_text) }
function printX (r_text) {
var Xml2String = r_text;
var myobj = (new Function("return " + Xml2String))()
tblvl = "xml_api_reply.finance";
tblvl = tblvl.split (".");
obj = myobj;
for (var tg in tblvl )
{
var xtag;
try {
xtag = obj.xtagName;
if (obj.xtagName == tblvl[tg])
{
//alert (obj.xtagName + obj.tags.length)
obj = obj.tags[0]
}
}
catch (e) {
//xtag = obj.xtagName;
}
}
alert (obj.xtagName)
alert (obj.tagAttrb.data) }
在行 - “alert (obj.xtagName)” 中给出上述错误消息。 不过,我也在警报对话框中看到了正确的输出。
我浏览了这个论坛中的各种帖子,但找不到类似的内容。 到目前为止我发现的是关于 - 格式错误的 JSON(带有额外的逗号) - 关于 jQuery 的担忧(这里我没有使用 jQuery)
我在这里缺少的一点是什么。我只是一个基本的 JS 用户。
其次,“xtagName”和“tagAttrb”都是来自同一 JSON 对象的对象。那么,什么会导致其中一个出错,而其他则正常。
更奇怪的是我有相同代码的另一个版本,该版本组织得不好,但代码工作正常。
请帮忙。
以下是 JSON 字符串的摘录
{"xtagName":"xml_api_reply",
"tagAttrb":{"version":"1"}
, "tags": [
{"xtagName":"finance",
"tagAttrb":{"module_id":"0", "tab_id":"0", "mobile_row":"0", "mobile_zipped":"1", "row":"0", "section":"0"}
, "tags": [
{"xtagName":"symbol",
"tagAttrb":{"data":".BSEREAL"}
, "tags": []},
{"xtagName":"pretty_symbol",
"tagAttrb":{"data":".BSEREAL"}
, "tags": []},
{"xtagName":"daylight_savings",
"tagAttrb":{"data":"true"}
, "tags": []}]}]}
I have an issue, where for the same Object give the value, in the alert window as well as gives JS error in the browser.
At line - "alert (obj.xtagName)" gives the error message.
Though, I also see the correct output, in the alert dialog too.
Quite strange for me.
Using
Browser: Chrome (v10.0.648.151)
Error - Uncaught TypeError: Cannot read property 'xtagName' of undefined
Browser: IE (v8.0.7600.16385CO)
Error - 'obj.xtagName' is null or not an object
Object with error - obj.xtagName
-
I have PHP page (gXML.php) which generates JSON string. This JSON is also validated using "http://www.jsonlint.com/".
Using AJAX call, I get this JSON and convert it into JSON Object based on responseText.
-Start of File: _ajax.js
AJAX code call
function gQuote_getData() {
var request = _getHTTPObject();
var url = "php/gXML.php";
if (request) {
request.onreadystatechange = function() {gQuote_parseResponse(request);};
request.open( "GET", url, true );
request.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 1900 00:00:00 GMT");
request.send(null);
return true;
} else {
return false;
} }
XMLRespObj
function _getHTTPObject() {
var xhr = false;
if (window.XMLHttpRequest) {
xhr = new XMLHttpRequest();
} else if (window.ActiveXObject) {
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
} catch(e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
} catch(e) {
xhr = false;
}
}
}
return xhr;}
Receive Response
function gQuote_parseResponse(request) {
var resp;
if (request.readyState == 4) {
if (request.status == 200 || request.status == 304) {
//Helps trim extra space at beginning and end of contruct
resp = (request.responseText.replace(/^\s+|\s+$/g, ''));
}
}
fillOutput (resp);
//printX(gQuote_JSON);}
- End of File js/_ajax.js
-
HTML page JS script
function fillOutput (resp_text) {
document.getElementById("output").value = resp_text
printX(resp_text) }
function printX (r_text) {
var Xml2String = r_text;
var myobj = (new Function("return " + Xml2String))()
tblvl = "xml_api_reply.finance";
tblvl = tblvl.split (".");
obj = myobj;
for (var tg in tblvl )
{
var xtag;
try {
xtag = obj.xtagName;
if (obj.xtagName == tblvl[tg])
{
//alert (obj.xtagName + obj.tags.length)
obj = obj.tags[0]
}
}
catch (e) {
//xtag = obj.xtagName;
}
}
alert (obj.xtagName)
alert (obj.tagAttrb.data) }
At line - "alert (obj.xtagName)" gives the above error message.
Though, I also see the correct output, in the alert dialog too.
I went through various threads in this forum, but could not locate, anything similar.
What i have found so far is about
- malformed JSON (w extra comma)
- concerns about jQuery (I am not using jQuery, here)
What is the point that I am missing here. I just a basic JS user.
Second, both "xtagName" and "tagAttrb" are objects from the same JSON object. So, what would be causing one of them into error whereas other works fine.
What is even more strange is I have another version of the same code, which was not well organized, there the code works fine.
Kindly help.
Below is extract of JSON String
{"xtagName":"xml_api_reply",
"tagAttrb":{"version":"1"}
, "tags": [
{"xtagName":"finance",
"tagAttrb":{"module_id":"0", "tab_id":"0", "mobile_row":"0", "mobile_zipped":"1", "row":"0", "section":"0"}
, "tags": [
{"xtagName":"symbol",
"tagAttrb":{"data":".BSEREAL"}
, "tags": []},
{"xtagName":"pretty_symbol",
"tagAttrb":{"data":".BSEREAL"}
, "tags": []},
{"xtagName":"daylight_savings",
"tagAttrb":{"data":"true"}
, "tags": []}]}]}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
obj
是一个匿名函数,而不是相当于 JSON 脚本的对象。请参阅以下代码中引用的第 1 行和第 4 行。obj
is an anonymous function, and not the object equivalent to the JSON script. See below lines 1 and 4 as quoted from your code.