Javascript/appcelerator - 无法检测 XML
我正在尝试使用一个名为 appcelerator titan 的框架来制作一个简单的 iphone 应用程序。我试图引入 XML 源并简单地测试其长度,但没有返回任何内容(并且没有抛出任何错误)。我不明白发生了什么事。如果我将此 XML URL 替换为:
,例如,这个:
http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Superfad
我可以找到长度就好,所以我猜它是某种跨域问题,或者格式错误的 XML,或者其他什么。这是我的代码:
var loader = Titanium.Network.createHTTPClient();
// Sets the HTTP request method, and the URL to get data from
//loader.open("GET","http://superfad.com/json/featured");
//loader.open("GET","http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Superfad");
loader.open("GET","http://superfad.com/work/rss");
//loader.open("GET","test.xml");
// Runs the function when the data is ready for us to process
loader.onload = function()
{
Ti.API.log('projects!'); //THIS WORKS
var projects = eval('('+this.responseText+')');
Ti.API.debug('length' + projects.length) //THIS DOES NOT
};
有什么问题吗?
I'm trying to use a framework called appcelerator titanium to make a simple iphone app. I'm trying to bring in an XML source and simply test its length, but nothing is being returned (and no errors are being thrown). I can't figure out what's going on. If I swap this XML URL out:
for, say, this one:
http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Superfad
I can find the length just fine, so I'm guessing it's either some sort of crossdomain issue, or malformed XML, or something. Here's my code:
var loader = Titanium.Network.createHTTPClient();
// Sets the HTTP request method, and the URL to get data from
//loader.open("GET","http://superfad.com/json/featured");
//loader.open("GET","http://api.twitter.com/1/statuses/user_timeline.json?screen_name=Superfad");
loader.open("GET","http://superfad.com/work/rss");
//loader.open("GET","test.xml");
// Runs the function when the data is ready for us to process
loader.onload = function()
{
Ti.API.log('projects!'); //THIS WORKS
var projects = eval('('+this.responseText+')');
Ti.API.debug('length' + projects.length) //THIS DOES NOT
};
Any ideas what's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在尝试将 rss feed 的 xml 评估为 json。您的第一个链接返回 xml,第二个链接返回 json。
eval
适用于 json,但不适用于 xml。请注意,不要使用eval
来解析 json。使用 JSON.parse。you are trying to evaluate the xml of the rss feed as json. Your first link returns xml, your second links returns json.
eval
will work on json but not xml. As a note, don't useeval
to parse json. useJSON.parse
.