JSON object error Uncaught TypeError: Cannot read property of undefined
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])
开发者_如何学Go {
//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": []}]}]}
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.
1 var myobj = (new Function("return " + Xml2String))()
2 tblvl = "xml_api_reply.finance";
3 tblvl = tblvl.split (".");
4 obj = myobj;
精彩评论