【问题标题】:Phonegap ajax GET returning Internal Server ErrorPhonegap ajax GET返回内部服务器错误
【发布时间】: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


【解决方案1】:

刚刚在我的电脑上试了一下,结果如下:

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 进行配置。您不仅必须允许您的脚本访问其他域,而且还应将您的服务器配置为可以从其他域访问。

【讨论】:

    【解决方案2】:

    您是否尝试过通过 ajax GET 获取 xml 并转换为 JSON 以便更轻松地处理?

    也许这有助于找出错误。

    【讨论】:

    • $.ajax({ url: "cbccork.ie/…", }) .done(function( data ) { console.log("数据样本:", data ); }) .fail( function(jqXHR, textStatus, errorThrown) { console.log('error throwen is '); console.log(errorThrown); });
    • 我认为可能是服务器配置问题。
    • 你也许是因为我注意到该网站在移动设备上无法正常运行
    【解决方案3】:

    Joomla Mobile 插件已打开但未正确配置,这意味着从移动设备查询时未返回任何 xml。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-09-16
      • 2019-11-12
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多