简体   繁体   English

TypeError Obj.root是未定义的Ajax jQuery

[英]TypeError Obj.root is undefined ajax jquery

I am getting a console log output saying TypeError: treeObj.root is undefined on this line: var count = treeObj.root.length; 我收到一条控制台日志输出,提示TypeError: treeObj.root is undefined在此行上TypeError: treeObj.root is undefinedvar count = treeObj.root.length; not sure why or how to solve it? 不知道为什么或如何解决?

   $.ajax({
        url: 'Content/data.txt',
        dataType: 'text',
        success: function (treeObj) {

            var count = treeObj.root.length; //here

with dataType: 'text' you tell the ajax-call to treat the answer from the server as a plain string. 使用dataType: 'text'您告诉ajax调用将来自服务器的答案视为纯字符串。 Try changing that to dataType: 'json' 尝试将其更改为dataType: 'json'

treeObj is most likely getting passed in as a string, not an object so you need to parse the string into an object using something along: treeObj最有可能以字符串而不是对象的形式传入,因此您需要使用以下内容将字符串解析为对象:

try{
   treeObj = jQuery.parseJSON(treeObj);
}catch(e){}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM