-2

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 code:

$('#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>

2
  • You forgot a ; in your var declaration
    – Andrew Li
    Jun 11, 2016 at 1:27
  • @AndrewL True, but not causing the problem-it'd be optional there, no? The issue is in the params being sent to the Ajax function. Jun 11, 2016 at 1:30

1 Answer 1

0

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.

1
  • Thanks very much! That fixed the problem. I haven't used the userlinks just yet because I plan on adding it in later to create content.
    – trav
    Jun 11, 2016 at 1:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.