Datsik
Datsik

Reputation: 14824

Getting error Uncaught TypeError: string is not a function

I'm using this code here for a webpage I'm fooling around with:

$(function() {
    console.log('Here');
    $.ajax({
        type: 'POST',
        url: 'http://api.bf3stats.com/pc/playerlist/',
        data: {
            players: ['xxx', 'xxxx', 'x']
        },
        success: function(data) {
            loadData(data, 'x', 'f');

        }
    })
})

function loadData(data, uname, $) {
    $('#fWins').text("135");
}

But once it hits the loadData function I get the error:

Error: Uncaught TypeError: string is not a function

I'm absolutely clueless as to why this is giving me so much problems. Thanks in advance!

Thanks problem was me using $, I don't use jQuery often, only for when i need cross browser compatibility. Thanks for this, will mark Answered when timer is up

Upvotes: 4

Views: 2895

Answers (3)

clav
clav

Reputation: 4251

It's because you're using $ as the parameter name for the last param in loadData. This means your function will use the string "f" that you're passing to the function when it sees the $ on $('#fWins').text("135"); instead of using jQuery, so it's essentially doing this: "f"('#fWins').text("135"); To fix it, change:

function loadData(data, uname, $) {

to something like

function loadData(data, uname, lastParam) {  

Upvotes: 2

Patrick McWilliams
Patrick McWilliams

Reputation: 143

the $ is trying to pass that variable as a function so if you had a function declared with the string being passed it would fire it

any reason why you want $ there?

Upvotes: 0

palaѕн
palaѕн

Reputation: 73936

Try this:

$(function () {
    console.log('Here');
    $.ajax({
        type: 'POST',
        url: 'http://api.bf3stats.com/pc/playerlist/',
        data: {
            players: ['xxx', 'xxxx', 'x']
        },
        success: function (data) {
            loadData(data, 'x', 'f');

        }
    });
});

function loadData(data, uname, param) {
    console.log('Here too..');
    $('#fWins').text("135");
}

FIDDLE ( In the console you will get both Here & Here too.., without any error )

Upvotes: 1

Related Questions