Skip to content

Conversation

@zherunliu
Copy link
Contributor

屏幕截图 2025-12-25 204347

例如使用 axios 时,开发者可能不指定 headers 中的 content-type,导致难以定位的 NPE 错误,建议开启 undefined 检查

@chouchouji
Copy link
Member

有个疑问,你说的 不指定 headers 中的 content-type 有没有具体的代码

image

不设置 content-type 前面就被拦截了

@zherunliu
Copy link
Contributor Author

debug 的情况是存在 ‘content-type’ 字段,但其值为 undefined,具体的代码大概如下,在调用 getArticleListApi 时出错

import axios from "axios";
import { UniAdapter } from "uniapp-axios-adapter";

const instance = axios.create({
  adapter: UniAdapter,
  timeout: 3000,
  baseURL: import.meta.env.VITE_API_BASE_URL,
});

export const getArticleListApi = () => instance.get("/article");

@chouchouji
Copy link
Member

debug 的情况是存在 ‘content-type’ 字段,但其值为 undefined,具体的代码大概如下,在调用 getArticleListApi 时出错

import axios from "axios";
import { UniAdapter } from "uniapp-axios-adapter";

const instance = axios.create({
  adapter: UniAdapter,
  timeout: 3000,
  baseURL: import.meta.env.VITE_API_BASE_URL,
});

export const getArticleListApi = () => instance.get("/article");

这个问题是不是 uniapp-axios-adapter 带来的?正常web端 content-type 也不会设置为 undefined 吧

@zherunliu
Copy link
Contributor Author

这个问题是不是 uniapp-axios-adapter 带来的?正常web端 content-type 也不会设置为 undefined 吧

image

axios 这里已经是 undefined 了

@zherunliu
Copy link
Contributor Author

这个问题是不是 uniapp-axios-adapter 带来的?正常web端 content-type 也不会设置为 undefined 吧

axios Content-Type 默认值是:
POST/PUT/PATCH 有默认值 application/x-www-form-urlencoded,GET/DELETE
请求体为对象 / 数组时,Content-Type 默认为 application/json

@chouchouji chouchouji merged commit fdd9b4a into dcloudio:next Dec 29, 2025
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants