使用 Opera 和 Chrome 浏览器管理 XML/Ajax

发布于 2024-12-17 14:23:16 字数 1385 浏览 1 评论 0原文

首先,抱歉我的英语不好,我是卡斯蒂利亚人...

我需要这方面的帮助:我有一个网络,可以从 XML 文件中捕获数据。

我的代码是这样的:

$(function() {
    $.ajax({
        type: "GET",
        cache: false,
        dataType: ($.browser.msie) ? "text" : "xml",
        success: parseXML,
        error: function(){alert("Error: Something went wrong");}
    });
});


function parseXML(){
    var xml = null;
    if (typeof window.ActiveXObject != 'undefined' )
    {   // code for IE
        xml = new ActiveXObject( "Microsoft.XMLDOM" ); 
    }
    else if((typeof document.implementation != 'undefined' ) && (typeof document.implementation.createDocument != 'undefined'))
    {   // code for Mozilla, Firefox, Opera, etc.
        //xml = new XMLHttpRequest();
        xml = document.implementation.createDocument( "", "", null );
    }
    else{
        xml = new XMLHttpRequest();
    }

    xml.async=false;
    xml.resolveExternals = false;
    xml.validateOnParse = false;
    xml.load("dades.xml");

    total=$(xml).find('llibre').size();
    $(xml).find('llibre').each(function(){
            var desc = $(this).find('descripcio').text()
            var img = $(this).find('imatge').text();
            var url = $(this).find('url').text();
          ....

在这里我使用这个变量,但是在 Chrome 和 Opera 浏览器中我在 $.ajax({"Something goneError" 但在 Firefox 和 Internet Explorer 中它是有效的。

非常感谢您的任何答复

first of all, sorry form my poor english, I'm Castilian...

I need help with this: I have a web where I catch data from a XML file.

My code is this:

$(function() {
    $.ajax({
        type: "GET",
        cache: false,
        dataType: ($.browser.msie) ? "text" : "xml",
        success: parseXML,
        error: function(){alert("Error: Something went wrong");}
    });
});


function parseXML(){
    var xml = null;
    if (typeof window.ActiveXObject != 'undefined' )
    {   // code for IE
        xml = new ActiveXObject( "Microsoft.XMLDOM" ); 
    }
    else if((typeof document.implementation != 'undefined' ) && (typeof document.implementation.createDocument != 'undefined'))
    {   // code for Mozilla, Firefox, Opera, etc.
        //xml = new XMLHttpRequest();
        xml = document.implementation.createDocument( "", "", null );
    }
    else{
        xml = new XMLHttpRequest();
    }

    xml.async=false;
    xml.resolveExternals = false;
    xml.validateOnParse = false;
    xml.load("dades.xml");

    total=$(xml).find('llibre').size();
    $(xml).find('llibre').each(function(){
            var desc = $(this).find('descripcio').text()
            var img = $(this).find('imatge').text();
            var url = $(this).find('url').text();
          ....

And here I work with this vars, but in Chrome and Opera browsers I have the error in $.ajax({"Something went wrong"
But in Firefox and Internet Explorer it that works.

A lot of thanks for any answer

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我的鱼塘能养鲲 2024-12-24 14:23:16

AJAX 调用似乎是正确的,并且当我执行它时,我在 Chrome 控制台中没有看到任何错误。您能发布您收到的完整错误吗?我认为错误是在 parseXML 函数中而不是在 AJAX 调用中。

The AJAX call seems right and I see no error in the Chrome Console when I execute it. Could you post the full error you get? I think the error is in the parseXML function and not in the AJAX call.

沧笙踏歌 2024-12-24 14:23:16

我的完整 parseXML 函数:

function parseXML(){
    var xml = null;
    if (typeof window.ActiveXObject != 'undefined' )
    {   // code for IE
        xml = new ActiveXObject( "Microsoft.XMLDOM" ); 
    }
    else if((typeof document.implementation != 'undefined' ) && (typeof document.implementation.createDocument != 'undefined'))
    {   // code for Mozilla, Firefox, Opera, etc.
        //xml = new XMLHttpRequest();
        xml = document.implementation.createDocument( "", "", null );
    }
    else{
        xml = new XMLHttpRequest();
    }
    xml.async=false;
    xml.resolveExternals = false;
    xml.validateOnParse = false;
    xml.load("dades.xml");

    total=$(xml).find('llibre').size();
    $(xml).find('llibre').each(function(){
        var id_text = $(this).attr('id');                                           
        if((id_text>=3*(index-1)) && (id_text<3*index)){
            var desc = $(this).find('descripcio').text()
            var img = $(this).find('imatge').text();
            var url = $(this).find('url').text();

            if(id_text%3==0){

                //Aquest es el primer element que sortira a la pantalla, cal eliminar els altres abans dels altres per si no s'emplena la pantalla
                $('#imatge1').empty();
                $('#imatge2').empty();
                $('#imatge3').empty();
                $('#text1').empty();
                $('#text2').empty();
                $('#text3').empty();

                if(url==""){
                    //Quan no hi ha url en el arxiu xml, no afegim link a la imatge
                    $('<li>').html('<img src="'+img+'"/>').appendTo('#imatge1');
                }
                else{
                    $('<li>').html('<a href="' + url + '"><img src="'+img+'"/></a>').appendTo('#imatge1');
                }
                //$("#text1").append(desc);
                $('<li>').html(desc).appendTo('#text1');
                if(url!=""){
                    $('<li>').html('<a href="' + url + '"> Link </a>').appendTo('#text1');
                }
            }
            else if(id_text%3==1){
                if(url==""){
                    $('<li>').html('<img src="'+img+'"/>').appendTo('#imatge2');
                }
                else{
                    $('<li>').html('<a href="' + url + '"><img src="'+img+'"/></a>').appendTo('#imatge2');
                }
                $('<li>').html(desc).appendTo('#text2');
                if(url!=""){
                    $('<li>').html('<a href="' + url + '"> Link </a>').appendTo('#text2');
                }
            }
            else if(id_text%3==2){
                if(url==""){
                    $('<li>').html('<img src="'+img+'"/>').appendTo('#imatge3');
                }
                else{
                    $('<li>').html('<a href="' + url + '"><img src="'+img+'"/></a>').appendTo('#imatge3');
                }
                $('<li>').html(desc).appendTo('#text3');
                if(url!=""){
                    $('<li>').html('<a href="' + url + '"> Link </a>').appendTo('#text3');
                }
            }
        }
    }); //close each(
}

我认为函数的其余部分并不重要,因为我只将 de vars 放在 html 标签中......

My full parseXML function:

function parseXML(){
    var xml = null;
    if (typeof window.ActiveXObject != 'undefined' )
    {   // code for IE
        xml = new ActiveXObject( "Microsoft.XMLDOM" ); 
    }
    else if((typeof document.implementation != 'undefined' ) && (typeof document.implementation.createDocument != 'undefined'))
    {   // code for Mozilla, Firefox, Opera, etc.
        //xml = new XMLHttpRequest();
        xml = document.implementation.createDocument( "", "", null );
    }
    else{
        xml = new XMLHttpRequest();
    }
    xml.async=false;
    xml.resolveExternals = false;
    xml.validateOnParse = false;
    xml.load("dades.xml");

    total=$(xml).find('llibre').size();
    $(xml).find('llibre').each(function(){
        var id_text = $(this).attr('id');                                           
        if((id_text>=3*(index-1)) && (id_text<3*index)){
            var desc = $(this).find('descripcio').text()
            var img = $(this).find('imatge').text();
            var url = $(this).find('url').text();

            if(id_text%3==0){

                //Aquest es el primer element que sortira a la pantalla, cal eliminar els altres abans dels altres per si no s'emplena la pantalla
                $('#imatge1').empty();
                $('#imatge2').empty();
                $('#imatge3').empty();
                $('#text1').empty();
                $('#text2').empty();
                $('#text3').empty();

                if(url==""){
                    //Quan no hi ha url en el arxiu xml, no afegim link a la imatge
                    $('<li>').html('<img src="'+img+'"/>').appendTo('#imatge1');
                }
                else{
                    $('<li>').html('<a href="' + url + '"><img src="'+img+'"/></a>').appendTo('#imatge1');
                }
                //$("#text1").append(desc);
                $('<li>').html(desc).appendTo('#text1');
                if(url!=""){
                    $('<li>').html('<a href="' + url + '"> Link </a>').appendTo('#text1');
                }
            }
            else if(id_text%3==1){
                if(url==""){
                    $('<li>').html('<img src="'+img+'"/>').appendTo('#imatge2');
                }
                else{
                    $('<li>').html('<a href="' + url + '"><img src="'+img+'"/></a>').appendTo('#imatge2');
                }
                $('<li>').html(desc).appendTo('#text2');
                if(url!=""){
                    $('<li>').html('<a href="' + url + '"> Link </a>').appendTo('#text2');
                }
            }
            else if(id_text%3==2){
                if(url==""){
                    $('<li>').html('<img src="'+img+'"/>').appendTo('#imatge3');
                }
                else{
                    $('<li>').html('<a href="' + url + '"><img src="'+img+'"/></a>').appendTo('#imatge3');
                }
                $('<li>').html(desc).appendTo('#text3');
                if(url!=""){
                    $('<li>').html('<a href="' + url + '"> Link </a>').appendTo('#text3');
                }
            }
        }
    }); //close each(
}

I think that the rest of the function doesn't matter because I only put de vars in html tags...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文