0

This is my first attempt at javascript trying to call to API and I'm getting the following in the Chrome console:

Uncaught syntax error: Unexpected Token ,

Uncaught Reference Error: AjaxCall is not defined.

I just posted here today, edited the answer to my liking, and got more errors.

Thanks for helping!

<!DOCTYPE html>
<html>
    <head>
    </head>
    <body>
        <button onclick="AjaxCall()">Click Here</button>
        <p id="Data"></p>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        <script>
            var vToken = "MyToken";
            var Target_URL = "MyUrl";

            function AjaxCall() {
                $.ajax({
                    url: Target_URL,
                    headers: {
                        'Accept': 'application/json',
                        'Authorization',
                        'Bearer ' + vToken
                    },
                    method: 'GET'
                    success: alert(result)
                });
            }
        </script>
    </body>
</html>
Community
  • 1
  • 1
Arch1medes
  • 39
  • 1
  • 8

3 Answers3

3

Here is your completed code. Should work fine with right urls and headers.

<!DOCTYPE html>
        <html>
        <head>
        </head> 
        <body>

        <button onclick="AjaxCall()">Click Here</button>
        <p id="Data"></p>


        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
        <script>
        var vToken = "MyToken";
        var Target_URL = "MyUrl";


        function AjaxCall(){
         $.ajax({
            url: Target_URL,
            headers: {
                'Accept':'application/json',
                'Authorization': 'Bearer ' + vToken
            },
            method: 'GET',
            success: function(result){
                alert(result);
            }
          }); 

        }
        </script>

        </body>

        </html>
Ashraful Alam
  • 380
  • 1
  • 12
1

You have a simple syntax error in this section of your code:

$.ajax({
    url: Target_URL,
    headers: {
        'Accept':'application/json',
        'Authorization', 'Bearer ' + vToken
    },
    method: 'GET'
    success: alert(result)
});

This line:

'Authorization', 'Bearer ' + vToken

ought to be

'Authorization': 'Bearer ' + vToken

with a colon : rather than a comma ,. That's what the Uncaught syntax error: Unexpected Token , error meant.


Also, you're missing a comma after 'GET' (thanks to @char):

method: 'GET'

should be

method: 'GET',
Hatchet
  • 5,320
  • 1
  • 30
  • 42
0
         something wrong with your Authorization params, please keep headers correct
         $.ajax({
            url: Target_URL,
            headers: {
                'Accept':'application/json',
                'Authorization': 'Bearer ' + vToken
            },
            method: 'GET'
            success: alert(result)
          });
  • It's a cURL to Ajax conversion based from the this post: http://stackoverflow.com/questions/10093053/add-header-in-ajax-request-with-jquery – Arch1medes Mar 30 '16 at 01:34
  • success should be a callback function, where result is passed as parameter. Please check my answer. – Ashraful Alam Mar 30 '16 at 01:35