我使用Phonegap和Backbone构建一个应用程序来解析外部XML提要。 Feed位于:
http://cbccork.schoolspace.ie/index.php?option=com_ninjarsssyndicator&feed_id=1&format=raw
我用:
获取var news = new model.NewsCollection();
news.fetch({
full_url: true,
success: function (collection) {
slider.slidePage(new NewsList({collection: collection}).$el);
},
error: function (model, response, options) {
console.log('statusText is ');
console.log(response.statusText);
console.log('responseText is ');
console.log(response.responseText);
},
});
这很好用。但是,子域名将很快被删除,因此Feed网址将变为:
http://cbccork.ie/index.php?option=com_ninjarsssyndicator&feed_id=1&format=raw
如果你转到网址,你会看到输出是相同的xml(http://cbccork.ie/而不是http://cbccork.schoolspace.ie/)。
但是,在Android设备上进行测试时,不会返回任何内容。我打印出了响应:
Value of responseText is
Value of responseXML is null
Value of status is 500
Value of statusText is Internal Server Error
我在chrome中测试了这个(通过禁用相同的原始策略)并且它有效。但在任何Android设备上,它都无法正常工作。
我一直试图解决这个问题3天,我完全被难倒了。有任何想法吗?
修改
新闻模型和集合如下所示:
define(function (require) {
"use strict";
var $ = require('jquery'),
Backbone = require('backbone'),
id=1,
xml,
parsed = [],
title = "",
description = "",
pubDate = "",
src="",
img="",
News = Backbone.Model.extend({
}),
NewsCollection = Backbone.Collection.extend({
model: News,
//url: 'http://www.test.webintelligence.ie/test/',
url: 'http://www.cbccork.ie/index.php?option=com_ninjarsssyndicator&feed_id=1&format=raw',
//This is used so I can test on a browser. On a device, use the direct link
/*
url: function(){
console.log('in news');
return "/school-proxy.php?type=news";
},*/
parse: function (data) {
xml = data;
$(xml).find('item').each(function (index) {
img = $(description).find('img:first');
src = img.attr('src');
if(typeof(src)==='undefined' || src===null || src===""){
//so its null or undefined
src = "img/crest.jpg";
}
title = $(this).find('title').text();
description = $(this).find('description').text();
pubDate = $(this).find('pubDate').text();
pubDate = pubDate.substring(0, pubDate.length-12);
parsed.push({id:id, title: title,
description:description, pubDate:pubDate, src:src});
title, description, pubDate, src, img = "";
id++;
});
return parsed;
},
fetch: function (options) {
options = options || {};
options.dataType = "xml";
return Backbone.Collection.prototype.fetch.call(this, options);
}
});
return {
News: News,
NewsCollection: NewsCollection
};
});
修改
我尝试了直接的Ajax调用,但我再次在Android设备上遇到内部服务器错误:
$.ajax({
url: "http://www.cbccork.ie/index.php?option=com_ninjarsssyndicator&feed_id=1&format=raw",
})
.done(function( data ) {
console.log( "Sample of data:", data );
})
.fail(function( jqXHR, textStatus, errorThrown ) {
console.log('in the fail, textstatus is ');
console.log(textStatus);
console.log('error throwen is ');
console.log(errorThrown);
});
有什么方法可以解决这个问题吗?我已经尝试了一切......
答案 0 :(得分:2)
刚从我的电脑上尝试过,这就是结果:
XMLHttpRequest cannot load http://www.cbccork.ie/index.php?option=com_ninjarsssyndicator&feed_id=1&format=raw. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://192.168.2.121' is therefore not allowed access.
毫无疑问,您的服务器未配置为CORS。您不仅必须允许脚本访问其他域,还应配置您的服务器以便从其他域访问。
答案 1 :(得分:1)
您是否尝试通过ajax GET获取xml并将其转换为JSON for hadle更容易?
也许有助于找到错误。
答案 2 :(得分:0)
Joomla Mobile插件已打开但未正确配置,表示从移动设备查询时未返回xml。