Home > database >  Uncaught SyntaxError: expected expression, got ','
Uncaught SyntaxError: expected expression, got ','

Time:09-16

Im getting this error and I don't understand why. The js seems to be right to me. Clearly Im missing something. Any help would be appreciated.

JS:

            var doPostBack = function(UserId, TokenKey, opcao)
            {
                $.ajax({
                type: ""POST"",
                url: ""https://localhost:44382/Home/do_PostBack"", 
                data: {UserId: UserId, TokenKey: TokenKey, opcao: opcao}, 
                contentType: ""application/json; charset=utf-8"",
                success: function() {
                    
                    },
                failure: function() {
                        
                    },
                error: function() {
                        
                    }
                });
            }

HTML:

<a href="javascript:do_PostBack({TokenId}, {TokenKey}, {11})">

CodePudding user response:

Mismatch function name and parameter

<a href="javascript:do_PostBack(TokenId, TokenKey, 11)">Api Call</a>

JSON.stringify data before sending. You can also use property shorthand.

var do_PostBack = function(UserId, TokenKey, opcao)
{
    console.log(UserId, TokenKey, opcao);
    $.ajax({
        type: "POST",
        url: "https://localhost:44382/Home/do_PostBack", 
        data: JSON.stringify({ UserId, TokenKey, opcao }), 
        contentType: "application/json; charset=utf-8",
        success: function() {

        },
        failure: function() {

        },
        error: function() {

        }
    });
}

CodePudding user response:

There are double quotation marks in the type, url and contentType fields. This would make it invalid.

Below is the fixed function. I also changed 'var'to 'let'. But make sure this would not brake anything in your application.

let doPostBack = function(UserId, TokenKey, opcao) {
  $.ajax({
    type: "POST",
    url: "https://localhost:44382/Home/do_PostBack",
    data: {
      UserId: UserId,
      TokenKey: TokenKey,
      opcao: opcao
    },
    contentType: "application/json; charset=utf-8",
    success: function() {

    },
    failure: function() {

    },
    error: function() {

    }
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

  • Related