Skip to content Skip to sidebar Skip to footer

Ajax Url Update

Im sure there is a simple solution for this but im having issues, I have an external ajax.js file that has an AJAX call like so: $(function() { $.ajax({ url: url,

Solution 1:

Strings are immutable in Javascript, meaning that url is going to be constant, even if you change ticker. Do this instead:

$(function() {
    $.ajax({
        url: "http:/blah" + ticker,
        success: function(data, status) {}
    });
});

EDIT: Now that I think about it, it has nothing to do with strings being immutable. Doing url = "http:/" + ticker doesn't create a closure with the value of ticker, it simply creates a new string value and stores it in url. After the new value is created, it's not linked to ticker in any way.

Solution 2:

As far as I can tell from the code you've posted, your ajax routine is only being called when the page is loaded, which explains why only the default url / ticker is shown in the results.

You need to wrap the ajax routine in a function and call it after the user has inputted a symbol:

In your ajax.js file:

functiongetSymbolInfo(ticker) { // consider passing ticker to this function instead of using a global var
    $.ajax({
        url: url + ticker,
        success: function( data, status ) {
           // show results
        }
    });
}

And from MyFunction:

functionmyFunction()
{
    var ticker = document.getElementById('userInput').value;
    getSymbolInfo(ticker);

}

Post a Comment for "Ajax Url Update"