weixin_33725272 2014-11-13 12:42 采纳率: 0%
浏览 10

jQuery XML解析器

I would like to have a html5 video player (basic tag), and call an external xml file in orderto load and play an ad before the main video.

This is currently working but I would like to have the cleanest and most multiplatform as possible:

function callAd() {
$.ajax({
    type: "GET",    
    url: myexternal_file,
    success: function(xml){
        $(xml).find('MediaFile[type="video/mp4"]').each(function(){
            var preroll = $(this)['context']['textContent'];
            var video = document.getElementById("video1");  
            video.src = preroll;
            video.load();
        });
    }
});
}

And as soon as the page loads, call the callAd function with my external xml. How should I proceed whenever the external url isn't acessible?

Thanks

EDIT: Sorry, the question isn't clear. I would like to know how to correctly parse a XML using javascript/jquery and how should I correctly listen for events coming from this function.

  • 写回答

1条回答 默认 最新

  • weixin_33698043 2014-11-13 13:28
    关注

    You should use the error function when the URL in not available. If you want to use the resulted data you have to parse it. jQuery.ajax will do this automatically for you if you specify the dataType.

    If the resulted data is not valid XML it will throw and error and script execution might end.

    var retry = true;
    $.ajax({
        type: "GET",    
        url: myexternal_file,
        dataType: 'xml', // use dataType to parse the result as appropriate 
        success: function(xml){
            $(xml).find('MediaFile[type="video/mp4"]').each(function(){
                var preroll = $(this)['context']['textContent'];
                var video = document.getElementById("video1");  
                video.src = preroll;
                video.load();
            });
        },
        error: function(jqXHR, textStatus, errorThrown){
            console.log("Whoops, couldn't load ads: " + textStatus + ' ' + errorThrown);
            // wanna try again? uncomment below
            // if(retry) { callAd(); retry = false; }
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学中在线排序的时间在线排序的在线LPT算法
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 lammps拉伸应力应变曲线分析
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试,帮帮忙吧