I am trying to make a private page dedicated to an Ajax request. Here is the simple request.
我正在尝试创建一个专用于Ajax请求的私有页面。这是简单的请求。
window.onload = loaded;
function loaded(){
var xhr = new XMLHttpRequest();
xhr.open('GET', '/data_annotations', true);
xhr.onload = function(){
if(this.status == 200){
var data = xhr.responseText;
console.log(JSON.parse(data));
} else {
console.log("Rip");
}
}
xhr.send();
//httpreq.abort();
}
Here is the node.js that it's running off of:
这是它运行的node.js:
...
app.get('/', function(req, res, next){
console.log("Connected Successfully.");
res.render('home');
});
app.get('/data_annotations', function(req, res, next){
if(req.xhr || req.headers.accept.indexOf('json') > -1) {
const mc = mongo.MongoClient;
const url = 'mongodb://localhost:27017/';
const dbName = 'practiceMDB';
console.log("Got Data Annotations.");
mc.connect(url, { useNewUrlParser: true }, (err, client) =>{
if(err){
console.log(err);
} else {
const db = client.db(dbName);
data = db.collection('DataAnnotations');
data.find({}).toArray(function(err, data){
res.send(data)
});
client.close();
}
});
} else {
res.redirect('/');
}
});
app.listen(port, function(){
console.log('Server Started on Port '+port);
});
I only want /data_annotaion to run if it's from the Ajax request. If a user types in /data_annotations in the url, it should redirect them to the home page. When I ran this I got these results:
我只希望/ data_annotaion运行,如果它来自Ajax请求。如果用户在url中键入/ data_annotations,则应将其重定向到主页。当我运行这个时,我得到了这些结果:
Server Started on Port 3000
Connected Successfully.
Connected Successfully.
This is indicating (to me) that the ajax request isn't registering as an ajax request, and is registering as a normal request. Further, I am getting this error:
这表明(对我来说)ajax请求没有注册为ajax请求,并且正在注册为普通请求。此外,我收到此错误:
Uncaught SyntaxError: Unexpected token < in JSON at position 0
I believe it is due to the redirection. The Ajax request gets redirected, it takes the response of the home page and is unable to parse it (I believe this to be happening because it cannot parse HTML text or string text - don't quote me on that). How do I get Node JS to register my Ajax request?
我相信这是由于重定向。 Ajax请求被重定向,它接受主页的响应并且无法解析它(我相信这会发生,因为它无法解析HTML文本或字符串文本 - 请不要引用我)。如何让Node JS注册我的Ajax请求?
PS: I looked at this answer to determine if a request is Ajax or not, but it always determines my requests as not Ajax: https://*.com/a/28540611/6804700
PS:我查看了这个答案以确定请求是否是Ajax,但它总是将我的请求确定为不是Ajax:https://*.com/a/28540611/6804700
1 个解决方案
#1
2
First thing - In your client-side code you need to set the accept header, because that is what you are looking for in your server side code.
第一件事 - 在客户端代码中,您需要设置接受标头,因为这是您在服务器端代码中寻找的内容。
xhr.setRequestHeader("accept", "application/json");
Second you can use the following code to return the data as json in your server side code
其次,您可以使用以下代码将数据作为json返回到服务器端代码中
res.json(data);
Another comment. It is bad practice to change the result type or redirect in an API. Your url is either returning JSON or redirecting to and HTML page which means the result is not consistent.
另一个评论。在API中更改结果类型或重定向是不好的做法。您的网址要么返回JSON,要么重定向到HTML页面,这意味着结果不一致。
#1
2
First thing - In your client-side code you need to set the accept header, because that is what you are looking for in your server side code.
第一件事 - 在客户端代码中,您需要设置接受标头,因为这是您在服务器端代码中寻找的内容。
xhr.setRequestHeader("accept", "application/json");
Second you can use the following code to return the data as json in your server side code
其次,您可以使用以下代码将数据作为json返回到服务器端代码中
res.json(data);
Another comment. It is bad practice to change the result type or redirect in an API. Your url is either returning JSON or redirecting to and HTML page which means the result is not consistent.
另一个评论。在API中更改结果类型或重定向是不好的做法。您的网址要么返回JSON,要么重定向到HTML页面,这意味着结果不一致。