VueJs 2,NodeJ和重新加载页面 [英] VueJs 2, NodeJs and reload page
问题描述
我对VueJ和刷新页面有疑问。当我使用特定的URL(例如:/ tag / 1)刷新VueJs应用程序时,发生错误:未捕获的SyntaxError:意外的令牌<。
我的服务器是用NodeJ创建的,我使用ExpressJ。我的模板引擎是EJS。
我已经定义了一条路线:
I have a question with VueJs and the refreshing page. When I refresh my VueJs app with specific URL (for example : /tag/1), an error occurs : "Uncaught SyntaxError: Unexpected token <". My server is created with NodeJs and I use ExpressJs. My templating engine is EJS. I have defined a route :
app.get('*', (request, response) => {
response.render('layouts/index')
})
在ExpressJs的文档中,可以使用插件 connect-history-api-fallback替换Apache的 .htaccess,但这不能正常工作。
In the documentation for ExpressJs, it's possible to use a plugin "connect-history-api-fallback" to replace ".htaccess" of Apache but this cannot work.
let app = express()
app.use(history({
verbose: true,
index: '/'
}))
问题是什么?
谢谢,
推荐答案
这是因为您的客户端应用程序(vue.js)希望收到一些有效的JSON对象,但您的服务器正在提供HTML页面。
This is happening because your client app (vue.js) is expecting to receive some valid JSON object, but your server is giving a HTML page.
尝试转换诸如< html>< head>之类的字符串时。 ..
放入一个javascript对象,您的JSON解析器将失败并给出该错误。要在本地复制此文件,请打开开发人员控制台并运行以下命令:
When attempting to convert some string like <html><head>...
into a javascript object, your JSON parser fails and gives that error. To replicate this locally, open your developer console and run the following command:
JSON.parse("<html></html>")
要找出到底出了什么问题,您需要查看开发者控制台的网络标签,并查看服务器对API请求的响应-您期望使用JSON,但服务器可能正在提供 index.html
To find out what exactly is going wrong, you need to look into the network tab of developer console and look at the server responses for API requests - you expect JSON but server might be serving index.html
假设服务器端一切正常,并按预期处理API请求,那么这可能是vue组件中的一个简单错误-而不是从 /加载标签数据api / tag / 1
(获取有效的JSON字符串),您可能正在尝试加载 / tag / 1
(这只会获取您的 index.html
)。
Assuming your server side is all good and handles API requests as expected, then it might be a simple error in your vue component - instead of loading your tag data from /api/tag/1
(which gets a valid JSON string), you might be attempting to load /tag/1
(which will only get your index.html
).
这篇关于VueJs 2,NodeJ和重新加载页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!