简体   繁体   中英

jQuery.get() not populating div

What's wrong with this function:

function() {
    $.get('/controller/action', function(data) {
        $('#temporaryPhotos').text(data);
    } );
    return false;
}

What it should do is fetch HTML from /controller/action page and insert the HTML into the #temporaryPhotos div on the current page.

Initial markup looks like this:

<div id="temporaryPhotos"></div>

So it's just an empty div. The jQuery function should fill it with photos form another page (it's the same website, of course). But the div stays empty.

EDIT:

I think I have not been very clear with my post so here is an additional information. What I am actually trying to accomplish is to use the abovementioned function as a callback for the Uploadify jquery plugin ( http://www.uploadify.com ).

Here is the full javascript code:

$(document).ready(function() {    
    $('#photo').uploadify({
        'uploader'       : '/flash-uploader/scripts/uploadify.swf',
        'script'         : '/flash-uploader/scripts/upload-public-photo.php',
        'cancelImg'      : '/flash-uploader/cancel.png',
        'scriptData'     : {'user_id' : 'USER_ID'},
        'queueID'        : 'fileQueue',
        'auto'           : true,
        'multi'          : true,
        'sizeLimit'      : 2097152,
        'fileExt'        : '*.jpg;*.jpeg;*.gif;*.png',
        'wmode'          : 'transparent',
        'onComplete'     : function() {
            $.get('/controller/action', function(data) {
                alert(data);
                $('#temporaryPhotos').html(data);
            } );
            return true;
        }
    });
});

I have tried both text() and html(), also alert(). Still nothing :(

EDIT2:

Upon further research I have found out that a default onComplete() function in the Uploadify plugin looks like this:

jQuery(this).bind("uploadifyComplete", {'action': settings.onComplete}, function(event, ID, fileObj, response, data) {
    if (event.data.action(event, ID, fileObj, unescape(response), data) !== false) {
        jQuery("#" + jQuery(this).attr('id') + ID + " .percentage").text(' - Completed');
        jQuery("#" + jQuery(this).attr('id') + ID).fadeOut(250, function() { jQuery(this).remove()});
    }
});

I have rewritten this function with my own (first one in this post) and that appears to be a problem.

EDIT3:

Head section of the page:

<script type="text/javascript" src="/js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="/flash-uploader/scripts/swfobject.js"></script>
<script type="text/javascript" src="/flash-uploader/scripts/jquery.uploadify.v2.1.0.min.js"></script>
<script type="text/javascript">
    //<![CDATA[
$(document).ready(function() {    
    $('#photo').uploadify({
        'uploader'       : '/flash-uploader/scripts/uploadify.swf',
        'script'         : '/flash-uploader/scripts/upload-public-photo.php',
        'cancelImg'      : '/flash-uploader/cancel.png',
        'scriptData'     : {'user_id' : 1},
        'queueID'        : 'fileQueue',
        'auto'           : true,
        'multi'          : true,
        'sizeLimit'      : 2097152,
        'fileExt'        : '*.jpg;*.jpeg;*.gif;*.png',
        'wmode'          : 'transparent',
        'onComplete'     : function() {
            alert("hello");
        }
    });
});    //]]>
</script><script type="text/javascript" src="/js/document-ready.js"></script>

Try using html instead of text:

 $('#temporaryPhotos').html(data);

Also, you might want to try alerting the response to see what's coming back from the server:

function() {
    $.get('/controller/action', function(data) {
        alert(data);
        $('#temporaryPhotos').html(data);
    });
    return false;
}

.text(str) inserts the str as plaintext (escaping the tags)

$("p").text("<b>Some</b> new text.");

<b>Some</b> new text.

http://docs.jquery.com/Attributes/text#val

.html(str) inserts it as html.

在Firebug中设置一个断点,以确保您没有滥用Uploadify。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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