How can I alter an external variable from inside my AJAX?

Posted by tmedge on Stack Overflow See other posts from Stack Overflow or by tmedge
Published on 2010-06-17T13:55:30Z Indexed on 2010/06/17 14:43 UTC
Read the original article Hit count: 259

Filed under:
|
|
|

I keep on having these same two problems. I have been trying to use Remy Sharp's wonderful tagSuggest plugin, and it works great. Until I try to use an AJAX call to get tags from my database.

My setGlobalTags() works great, with my defined myTagList at the top of the function. What I want to do is set myTagList equal to the result from my AJAX. My problem is that I can neither call setGlobalTags() from inside my success or error functions, nor actually alter the original myTagList.

Also, I keep on having this other issue as well. I keep this code in my Master page, and my AJAX returns 'success' on almost every page. I only (and always) get the error alert when I navigate to a page that actually contains something of id="ParentTags". I don't see why this is happening, because my $('#ParentTags').tagSuggest(); is definitely after my AJAX call.

I realize that this is probably just some dumb conventions error, but I am new to this and I'm here to learn from you guys. Thanks in advance!

$(function() {
        var myTagList = ['test', 'testMore', 'testALot'];

        $.ajax({
            type: "POST",
            url: 'Admin/GetTagList',
            dataType: 'json',
            success: function(resultTags) {
                myTagList = resultTags;
                alert(myTagList[0]);

                setGlobalTags(myTagList);
            },

            error: function() { 
                alert('Error');
                setGlobalTags(myTagList);
            }
        });

        setGlobalTags(myTagList);

        $('#ParentTags').tagSuggest();
    });

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery