Jamesking56
Jamesking56

Reputation: 3901

Semantic Issue. Unexpected Token :?

I'm attempting to do some AJAX using jQuery but I seem to be having an issue.

In my Safari inspector, I'm getting Semantic Issue and Unexpected Token :.

Am I just being stupid? It seems to be happening on my data: line of the $.ajax function:

function storeEmail ()
{
alert('Entered Function');

email = $('#emailField').val();
$('#emailField').hide();

console.log('Yep: '+email);

if(email.indexOf('@'))
{
    $.ajax(function(){
        url: "emailer/storeEmail.ajax.php",
        type: "POST",
        data: { "email" : email }
    }).done(function(data){
        if(data.resp == "success")
            $('#emailSuccess').show();
        else
        {
            $('#failedText').html('Error: '+data.resp);
            $('#emailFailed').show();
        }
    });
}
else
{
    $('#failedText').html('Error: Email is invalid!');
    $('#emailFailed').show();
}

return false;
}

Upvotes: 1

Views: 734

Answers (3)

Guffa
Guffa

Reputation: 700252

The reason that you get the error is this code:

function(){
    url: "emailer/storeEmail.ajax.php",
    type: "POST",
    data: { "email" : email }
}

It's an anonymous function, but the content is not code but the contents for an object literal.

As it's interpreted as code, the url: becomes a label, and "emailer/storeEmail.ajax.php" becomes an expression. After that comes the , operator that separates two expressions, so then it expects another expression, but instead comes type:. It interprets type as a variable, and the : token after it is what's unexpeted.

Just remove function() to make it an object literal:

{
    url: "emailer/storeEmail.ajax.php",
    type: "POST",
    data: { "email" : email }
}

Upvotes: 0

Musa
Musa

Reputation: 97672

You do not pass a function to $.ajax, you pass a map(JavaScript object)

$.ajax({
    url: "emailer/storeEmail.ajax.php",
    type: "POST",
    data: { "email" : email }
}).done(function(data){
    if(data.resp == "success")
        $('#emailSuccess').show();
    else
    {
        $('#failedText').html('Error: '+data.resp);
        $('#emailFailed').show();
    }
});

Upvotes: 1

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324620

You appear to be passing a function to $.ajax(). I think you meant to pass an object. Remove the function() piece and you should be good to go.

Upvotes: 3

Related Questions