简体   繁体   English

Ajax请求成功处理程序

[英]Ajax request success handler

I have a json implemented page where I am displaying a list of videos. 我有一个json实现的页面,我在其中显示视频列表。 I am getting the video <embed> codes as a javascript. 我正在将视频<embed>代码作为JavaScript。 While in the loop of creating the list, I use the $.ajax jquery function to initialize the javascript to get teh video flash player. 在创建列表的循环中,我使用$.ajax jquery函数初始化javascript以获取视频Flash Player。 but the problem is that the player is not getting appended to the supposed <div> . 但问题是播放器没有附加到假定的<div> Instead it gets appended at teh end of the document. 而是将其附加在文档的末尾。

$.ajax({
                url: item.ImagePath,
                dataType: "script",
                success: function(data){
                    alert(data);
                    var sResultFigure = $(document.createElement('figure')).append(result);
                }
            })

how do i solve this thing? 我该如何解决?

Well, you have to append it to the supposed div, like this: 好吧,您必须将其附加到假定的div上,如下所示:

var sResultFigure = $('<figure></figure>').append(result);
$('#supposedDiv').append(sResultFigure);

Assuming you are looping over divs and triggering an AJAX request for each div you could use the context parameter of the AJAX call to provide the current div to the success handler: 假设您正在遍历div并触发每个div的AJAX请求,则可以使用AJAX调用的context参数将当前div提供给成功处理程序:

$('div.foo').each(function(index, div) {
    $.ajax({
        url: item.ImagePath,
        dataType: 'script',
        context: div,
        success: function(data) {
            // here $(this) points to the div over which we were looping
            // when we triggered the AJAX request
            $(this).append($('<figure/>').append(result));
        }
    });
});

i assume your div's id is "holder". 我认为您的div的ID是“持有人”。

var sResultFigure = $("<figure></figure>").append(result);
$('#holder').append(sResultFigure);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM