AJAX rest调用错误”;意外的令牌<&”;
AJAX rest call error "unexpected token <"
我正试图在这个html页面中使用AJAX进行REST调用。首先,用户在/page上的搜索栏中输入一个术语,然后重定向到/test,但是ajax调用被打断了。API在控制台上运行时运行良好。我只是在努力将数据转移到/test页面,这样我就可以为用户设置样式并显示它。我正在使用node.js btw.
我得到的全部错误是:
Uncaught SyntaxError: Unexpected token < hello.js:1
它引用的是doctype声明。当我查看网页时,我注意到了一些东西。
<p> ID Passed: hello.js </p>
这在hello.js文件中。。。出于某种原因。搜索词仍然表现良好。它会说ID通过:选举
test.hbs
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="hello.js"></script>
<h2>This is the test!!!</h2>
<p>ID Passed: {{ output }} </p>
<p2 id = "tweets">
</p2>
hello.js
$(document).ready(function() {
$.ajax({
type: "GET", //I have tried deleting both type and dataType and still fails
dataType: "json", //I have also tried changing this to jsonp. No dice.
url: './srch.js'
}) //Push the data from calling srch.js into the "tweets" id on test.hbs
.done(function(data) {
document.getElementById("tweets").innerHTML=data;
})
.fail(function() {
alert("Ajax failed to fetch data")
})
});
srch.js->API的存储位置。
var Twit = require('twit');
var config = require('./views/config');
var sentiment = require('sentiment');
var T = new Twit(config);
var params = {
q: "election", //I know that the search term from the user isnt put in, I just want to see it print something on the page at all.
count: 1,
type: 'recent'
};
T.get('search/tweets', params, gotData);
function gotData(err, data, response) {
console.log(err);
//Output only the text of the json data returned by the search, and perform sentiment analysis with the sentiment module.
for(var i = 0; i<data.statuses.length; i++) {
document.write("Tweet " + (i + 1) + ":")
document.write("***********************")
document.write(data.statuses[i].text)
document.write(sentiment(data.statuses[i].text).score)
document.write("***********************")
}
}
我认为您正在返回html而不是json。我看到,有时当url无效时,或者当你没有经过身份验证时,你会得到html标记。
我会仔细检查ajax调用的返回,并将其显示在控制台中。
相关文章:
- 使用Javascript获取Twitter访问令牌
- FB.login访问令牌facebook javascript SDK
- React JS:未捕获(在承诺中)语法错误:在位置 0 的 JSON 中意外<令牌
- Phonegap:获取访问令牌时出现LinkedIn登录错误
- 将HTML(支持文件)中的令牌(字符串、数字等)传递给LogiXML
- 未捕获的语法错误:意外的令牌,
- 访问令牌和响应数据
- jQuery$.getJSON抛出意外令牌
- 访问令牌facebook未激活
- 如何使用Google撤销Oauth令牌'的Javascript API
- 当其他解析器认为意外的令牌有效时,json.parse会失败
- OAuth和访问令牌
- javascript api,用于在第三方域上存储身份验证令牌
- 意外的令牌模块生成失败:SyntaxError
- 为什么我在下面的..of循环中得到意外令牌
- 如何安全地获取&使用Facebook应用程序访问令牌发送通知使用PHP&Javascript
- AJAX rest调用错误”;意外的令牌<&”;
- 如何将CSRF令牌从AngularJS前端发送到Spring REST服务后端
- 如何实现OAuth 2.0,比如基于令牌的认证,用于从移动和javascript web应用程序访问的rest API
- 获取PDF通过REST API与头认证令牌,并显示在iframe