簡體   English   中英

為什么我的 fetch 正文總是在 Google Chrome 擴展程序中返回 null

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

現在我正在編寫這樣的 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);
        });

但結果正文總是得到{} 這是我的 api 結果文本:

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

我應該怎么做才能成功解析 http 響應上下文? 這是 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

在使用前調用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM