Remix Run中fetch返回空响应问题求助
解决方法
问题核心是**fetch返回的是Response对象,需要手动调用.json()方法解析响应体**。修改loadMerchantInfo函数如下:
const insecureAgent = new https.Agent({ rejectUnauthorized: false, }) export const loadMerchantInfo = async (cookie: string, options?: any) => { const actualOptions = options ? { ...options, } : ({} as RequestInit) if (process.env.NODE_ENV !== 'production') { actualOptions.agent = insecureAgent const headers = actualOptions.headers ? { ...options.headers, cookie, accept: 'application/json', } : { cookie, accept: 'application/json', } actualOptions.headers = headers } // 先获取Response对象 const response = await fetch('[Internal URL] ', actualOptions) // 校验请求是否成功(可选但推荐) if (!response.ok) { throw new Error(`请求失败,状态码:${response.status}`) } // 解析响应体为JSON return await response.json() }
修改后,merchantData会变为解析后的JSON对象,你可以根据需要在Loader函数中替换硬编码的返回值为return merchantData(需确保结构符合UnderwritingMerchantInfo类型)。
内容的提问来源于stack exchange,提问作者Jared Lee Jardine
相关产品推荐
相关产品推荐

