views:

227

answers:

3
+3  Q: 

jQuery.get() Help

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>
+6  A: 

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;
}
karim79
Exactly. Just clarifying, using text you are inserting the `data` argument inside `#temporaryPhotos` as a textNode, so escaping is automatically done by the browser.
Spidey
@Spidey - correct, opening "<" and closing ">" tags are replaced by their respective entities.
karim79
I have edited my post.
Richard Knop
@Richard Knop - if you run the $.get part alone, will something get returned from the server? First try to establish if the back-end is returning something or not.
karim79
Yes. I tried putting the $.get function on click() event on a link on the page, and then I do alert(data) and it returns correct content.
Richard Knop
I have edited the post second time.
Richard Knop
@Richard Knop - try replacing the 'onComplete' callback with this simple function: function() { alert('hello'); } and see if that fires
karim79
No that doesn't fire :(
Richard Knop
@Richard Knop - so we've isolated the problem to uploadify. Now for a really annoying question, have you included the uploadify.js *after* jQuery? Is it getting loaded in properly?
karim79
Yes it is. I have posted excerpt from the head section of the page in the first post.
Richard Knop
Yes it is loading properly because other features of the Uploadify are working fine, files are getting uploading ok, the backend PHP script is being executed etc...
Richard Knop
@Richard Knop - can you try including swfobject.js after jquery.uploadify.v2.1.0.min.js", see what happens (remember to keep the alert('hello') in place). This is turning out to be a real pain, but with a bit of luck everything will work out.
karim79
Ok I have solved the problem. The problem was at the end of the PHP backend file there must be line echo "1"; so the plugin knows when the uploading process is finished :);
Richard Knop
@Richard Knop - LOL! Feel better now?
karim79
Yeah, such stupid mistake. It's not mentioned in the plugin documentation :D
Richard Knop
+1  A: 

.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.

Dustin Getz
+1  A: 

Set a breakpoint in firebug to make sure you aren't misusing Uploadify.

Dustin Getz