Travis Johnson Travis Johnson - 6 months ago 30
jQuery Question

.success UncaughtSyntaxError: Unexpected identifier

I am truly baffled as to why I am receiving the Unexpected identifier error with my code. The only thing that was changed recently was the URL. I am working on a project and while waiting for my project partner to setup Heroku, I diverted to this fake api. Here's my



$('#username-submit').click(function() {
var userlinks = $('.user-links')
console.log('test');
$.ajax({
method: 'GET', //this is a GET git request
url: 'http://jsonplaceholder.typicode.com' //link to the API they created
beforeSend: function(xhr) {
xhr.setRequestHeader('Authorization', 'user name'); //takes the username and authorizes it
dataType: 'json',
.success(function(data) {
console.log(data);
})
}
})
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>




Answer

You've got the beforeSend handler a little bit wrong, or rather the brackets and the commas.

This should fix it:

$('#username-submit').click(function() {
  var userlinks = $('.user-links');
  console.log('test');
  $.ajax({
    method: 'GET', //this is a GET git request
    url: 'http://jsonplaceholder.typicode.com', //link to the API they created
    beforeSend: function(xhr) {
      xhr.setRequestHeader('Authorization', 'user name'); //takes the username and authorizes it
    },
    dataType: 'json',
    success: function(data) {
      console.log(data);
    }
  });
});

It also seems you're not using userlinks.

And to be frank, I doubt that "only the url changed" - the original version you've posted could never have worked in the first place.