【发布时间】:2013-12-20 10:20:20
【问题描述】:
我正在使用 Phonegap 和 Backbone 构建一个应用程序,它解析外部 XML 提要。提要位于:
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);
},
});
这很好用。但是,子域将很快被删除,因此提要 url 将变为:
http://cbccork.ie/index.php?option=com_ninjarsssyndicator&feed_id=1&format=raw
如果您转到 url,您将看到输出是相同的 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 中测试了这个(通过禁用同源策略)并且它有效。但在任何安卓设备上,它都不起作用。
我已经尝试解决这个问题 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);
});
有没有办法解决这个问题?我什么都试过了……
【问题讨论】:
-
只是一个想法 - 您是否在域白名单中正确列出了您的 URL?
-
是的,在 config.xml 我有
。我也尝试过其他网址,一切正常... -
在调用 fetch 方法之前如何声明和配置“news”,能不能多加点代码?
-
@homtg - 我已经编辑了这个问题。当我更改网址时,永远不会输入集合中的“解析”...
-
也许其他服务器需要一些第一个不需要的 HTTP 标头?
标签: jquery ajax backbone.js cordova