简体   繁体   English

为什么我的 fetch 正文总是在 Google Chrome 扩展程序中返回 null

[英]why my fetch body always return null in Google Chrome Extension

Now I am write a Google Chrome Extension code like this:现在我正在编写这样的 Google Chrome 扩展代码:

const apiUrl = defaultConfig.cruiseApi;
        const baseUrl = apiUrl + "/post/sub/source/add-by-plugin/";
        const urlParams ={
            subUrl:rssUrl
        }; 
        fetch(baseUrl,{
            method: 'POST',
            headers:{
                "Content-type": "application/json",
                "token": result.cruiseToken
            },
            body:JSON.stringify(urlParams)
        })
        .then(res => {
            console.info("the result is:"+ JSON.stringify(res.body));
            
        })
        .catch(error =>{
            console.error(error);
        });

but the result body always get {} .但结果正文总是得到{} this is my api result text:这是我的 api 结果文本:

costTime: "0"
ipAddress: null
msg: "login invalid"
result: null
resultCode: "200"
statusCode: "904"
validationErrors: null

what should I do to parse the http response context successfully?我应该怎么做才能成功解析 http 响应上下文? This is the curl command of http request:这是 http 请求的 curl 命令:

curl 'https://api.poemhub.top/post/sub/source/add-by-plugin/' \
  -H 'Connection: keep-alive' \
  -H 'Pragma: no-cache' \
  -H 'Cache-Control: no-cache' \
  -H 'sec-ch-ua: "Google Chrome";v="89", "Chromium";v="89", ";Not A Brand";v="99"' \
  -H 'DNT: 1' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36' \
  -H 'token: eyJ1c2VyX2lkIjoiNyJ9.657a56c26b4a3bbcaaa15a8afb46dc4e3c274f1e161823887960745d9f' \
  -H 'Content-type: application/json' \
  -H 'Accept: */*' \
  -H 'Origin: chrome-extension://jdnjeciaeelnmklhhcpihfekghplaiad' \
  -H 'Sec-Fetch-Site: none' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Accept-Language: en,zh-CN;q=0.9,zh;q=0.8,zh-TW;q=0.7,fr;q=0.6' \
  --data-raw '{"subUrl":"http://gerry.lamost.org/blog/?feed=rss2"}' \
  --compressed

invoke json function before use it:在使用前调用 json function :

const apiUrl = defaultConfig.cruiseApi;
        const baseUrl = apiUrl + "/post/sub/source/add-by-plugin/";
        const urlParams ={
            subUrl:rssUrl
        }; 
        fetch(baseUrl,{
            method: 'POST',
            headers:{
                "Content-type": "application/json",
                "token": result.cruiseToken
            },
            body:JSON.stringify(urlParams)
        })
        .then(res => {
           return res.json()
        })
        .then(res => {
            console.info("the result is:"+ JSON.stringify(res.body));
            
        })
        .catch(error =>{
            console.error(error);
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 Fetch body 总是为空 - Why is Fetch body going always null 为什么Chrome扩展程序不会返回Google Search API结果? - Why Chrome Extension wont return Google Search API results? 从主体返回 null 获取到 controller - Fetch from body return null into controller 为什么我的扩展程序在 Mozilla Firefox 上运行而不是在 Google Chrome 上运行? - Why is my extension working on Mozilla Firefox and not on Google Chrome? GET Fetch 工作但 POST Fetch 不工作 - Chrome 扩展中的 Google API - GET Fetch working but POST Fetch not working - Google APIs in Chrome Extension 为什么 chrome.pageCapture.saveAsMHTML 在我的 Google Chrome 扩展程序中不起作用? - Why isn't chrome.pageCapture.saveAsMHTML working in my Google Chrome Extension? 为什么我的 fetch() 发送一个空的 JSON 正文? - Why is my fetch() sending an empty JSON body? 在 chrome 扩展中使用 fetch 发送 null origin header - Using fetch in chrome extension sends null origin header 通过JSON的XMLHttpRequest POST响应始终为null(Chrome扩展程序) - XMLHttpRequest POST response via JSON always null (Chrome Extension) 为什么我的localStorage复选框/布尔值比较不能用于我的Google Chrome扩展程序? - Why is my localStorage checkbox/boolean value comparison not working for my Google Chrome Extension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM