2016-06-11 56 views
-2

我真的很困惑,为什么我收到我的代码出现'Unexpected identifier'错误。唯一最近更改的是URL。.success UncaughtSyntaxError:意外的标识符

我正在开发一个项目,在等待我的项目合作伙伴设置Heroku时,我转向了这个假API。

这里是我的代码:

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

+0

你在你的var声明中忘记了一个';' – Li357

+0

@AndrewL真的,但没有引起问题 - 它在那里是可选的,不是?问题在于发送给Ajax函数的参数。 –

回答

0

你已经得到了beforeSend处理程序有点不对劲,或者更确切地说,括号和逗号。

这应该修复它:

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

它也似乎你不使用userlinks

坦率地说,我怀疑“只有网址发生了变化” - 您发布的原始版本可能永远无法发挥作用。

+0

非常感谢!这解决了问题。我还没有使用'userlinks',因为我打算在稍后添加它来创建内容。 – trav