AJAX rest调用错误”;意外的令牌<&”;

AJAX rest call error "unexpected token <"

本文关键字:令牌 rest lt 意外 调用 AJAX 错误      更新时间:2023-09-26

我正试图在这个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调用的返回,并将其显示在控制台中。