本文介绍了“不推荐使用主线程上的同步 XMLHttpRequest"使用 nodejs app.get 或 http-server的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 AngularJS 中为 node.js 中的后端创建一个前端.我可以选择两个简单的 node.js 前端服务器来服务前端网页:一个是一个简单的 app.get in express,另一个是使用 http-server 包.

I am creating a front end in AngularJS for a backend in node.js. I have the option of two simple node.js front end servers to serve the front end web page: one is a simple app.get in express, the other is using the http-server package.

无论我使用哪个前端服务器代码,都会在 Chrome 中收到以下浏览器控制台消息:

Whichever front end server code I use, I get the following browser console message in Chrome:

主线程上的同步 XMLHttpRequest 已被弃用,因为它会对最终用户的体验产生不利影响.如需更多帮助,请查看 http://xhr.spec.whatwg.org/.

为了确保这不是由任何 Angular(或 Bootstrap)引起的,我已将我的网页缩减为以下内容:

To make sure that this was not caused by anything Angular (or Bootstrap) I have cut back my webpage to the following:

<!DOCTYPE html>
<html lang="en" >
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>zoneshark</title>
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
</html>

为什么我会收到错误消息?http-server 总是会导致这个错误吗?如何使用 nodejs 设置前端服务器,以免发生此错误(只需这个简单的hello world"页面作为起点)?

Why am I getting the error message? Does http-server always cause this error? How can I set up a front end server using nodejs so that this error does not occur (just this simple "hello world" page would be a starting point)?

我用来替代 http-server 的服务器代码是:

The server code I am using as an alternative to http-server is:

var express = require('express');
var app = express();

app.get('/', function(req,res) {
  res.sendFile('./index.html', {root: __dirname});
});

app.get(/^(.+)$/, function(req, res) {
  res.sendFile('./' + req.params[0], {root: __dirname});
});

app.listen(80);

另一个怪事.从 http-server 控制台,看起来 http-server 正在服务两个资源:/index.html 正如预期的那样,还有/favicon.ico - 这很奇怪,因为在任何地方都没有提到.

Another oddity. From the http-server console, it looks like http-server is serving two resources: /index.html as expected, but also /favicon.ico - which is odd as this is not mentioned anywhere.

最后一个奇怪的地方:这只发生在 Chrome 中.从 IE 看​​没有问题,也不需要 favicon.ico.

The final oddity: this only happens from Chrome. From IE there is no issue, and no favicon.ico is called for.

在 Chrome 中,我已清除所有浏览数据,但自动填充表单数据、密码和内容许可除外.

In Chrome, I have cleared all browsing data, except autofill form data, passwords and content licences.

推荐答案

感谢您的意见.我已经找到了这个问题,它在 Chrome 中——尤其是扩展.

Thanks for the comments. I have tracked down the issue, and it is within Chrome - more particularly extensions.

问题在于我有一个扩展:PropertyWizza v2.0.禁用此扩展程序可以解决问题.我现在将卸载它,这样它就不会干扰我的开发消息.

The issue is with an extension I have: PropertyWizza v2.0. Disabling this extension clears the problem. I will now uninstall it so it doesn't interfere with my development messages.

这都是推断出来的,因为我注意到,只要我使用 Chrome,我在家里的任何计算机上访问任何网站(例如 BBC 和 GitHub)时都会遇到同样的问题.

This was all deduced because I noticed that I had the same problem when accessing any website - including BBC and GitHub for instance - from any computer in my home, as long as I used Chrome.

对于第一次开始调试前端的任何人,我的建议是在开始之前检查您在所有浏览器中的其他网站上遇到的错误.这将为您的测试提供控制".

My advice to anyone starting to debug front-ends for the first time is to check what errors you get on other websites in all your browsers before you begin. This will provide the "control" for your testing.

这篇关于“不推荐使用主线程上的同步 XMLHttpRequest"使用 nodejs app.get 或 http-server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-20 12:21