接接口之你所需要知道的前后端知识
一些心路历程
复盘
1. Nginx 的 405 not allowed 错误解决
接口文档所写使用 POST 请求,但使用 POST 请求后报错 405
<html>
<head>
<title>405 Not Allowedtitle>
head>
<body bgcolor="white">
<center><h1>405 Not Allowedh1>center>
<hr />
<center>nginx/1.0.11center>
body>
html>
根据 -> Nginx 的 405 not allowed 错误解决
因为请求的静态文件采用的是 post 方法,nginx 是不允许 post 访问静态资源
于是采用 get 方式请求,不再报错 405,但是报错 Uncaught (in promise) error
2. VUE 中出现 Uncaught (in promise) error
接口 Status Code: ok,但是控制台报错 Uncaught (in promise) error
根据 -> VUE 中出现 Uncaught (in promise) error 怎么解决?
没有写 catch,强行将错误抛出了
xxxapi(params)
.then((res) => {})
.catch(() => {}) // 加上了这行
.finally(() => {});
控制台不再报错,但依然没有返回数据,试着输出错误信息
xxxapi(params)
.then((res) => {})
.catch((e) => {
console.log(e, "--e");
})
.finally(() => {});
// 输出 Uncaught (in promise) error ......
发现输出的即为控制台报错的东西 Uncaught (in promise) error …
...
的内容为 html 文档
3. 前端项目请求接口时返回的 response 是 html 文档
前端项目请求接口时返回的 response 是 html 文档怎们办?
遇到这种情况,一般是域名不对。
比如:正常请求这个接口需要用的是测试环境或者线上环境的域名,而你用的是 localhost 或者 127.0.0.1,
此时接口找不到资源就会返回当前项目的 index.html 文档了。
以为是本地 localhost 访问不了,发现其他接口都正常
仔细看才发现 访问的域名与接口文档不符
再把请求方式改为 POST 请求,🆗 欧克正常
所以问题来了,为什么接口文档写 POST 请求,
一开始用 POST,反而不行,用 GET 却可以呢