Cross-Origin 读取阻塞 (CORB) API-Call Chrome-extension

Cross-Origin Read Blocking (CORB) API-Call Chrome-extension

我想通过 post 从我的 chrome-extension 调用 api,问题是没有得到响应数据。

我刚刚在控制台中收到 Cross-Origin Read Blocking (CORB),响应为空。

Cross-Origin Read Blocking (CORB) blocked cross-origin response http://127.0.0.1:8080/api/v1/login/ldap with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.

首先我尝试直接调用 ajax:

$.ajax({
            type:    'POST',
            url:     'http://127.0.0.1:8080/api/v1/login/local',
            data:    postBody,
            success: function(resData, status, jqXHR) {
                console.log({resData, status, jqXHR});
            },
            error:   function(jqXHR, status) {
                console.log({jqXHR, status});
            }
        });

我试图将我的 ajax 呼叫从 content.js 移动到 background.js,就像这样

background.js

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        if(request.contentScriptQuery === 'login') {
            $.ajax({
                type:    'POST',
                url:     'http://127.0.0.1:8080/api/v1/login/local',
                data:    postBody,
                success: function(resData, status, jqXHR) {
                    sendResponse([
                        {
                            resData: resData,
                            status:  status,
                            jqXHR:   jqXHR
                        }, null
                    ]);
                },
                error:   function(jqXHR, status) {
                    sendResponse([
                        null, {
                            status: status,
                            jqXHR:  jqXHR
                        }
                    ]);
                }
            });
        }
//right here?
return true;
    });

content.js

chrome.runtime.sendMessage({contentScriptQuery: 'login'}, messageResponse =>{
            console.log(messageResponse);
        });

但在这种情况下,我收到以下错误:

"Unchecked runtime.lastError: The message port closed before a response was received."

而且我不知道如何保持端口打开并接收请求 body。或者即使我得到一个 body 或仍然得到 corb 问题。

这是我今天的最后一次尝试,端口仍然关闭:-(

"Unchecked runtime.lastError: The message port closed before a response was received."

chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
    if(request.contentScriptQuery === 'loginLocal') {
        $.ajax({
            type: 'POST',
            url:  'http://127.0.0.1:8080/api/v1/login/local',
            data: postBody
        }).then(function(resData, status, jqXHR) {
            sendResponse([
                {
                    statuscode: jqXHR.status,
                    resData:    resData,
                    status:     status
                }, null
            ]);
        }, function(jqXHR, status) {
            sendResponse([
                null, {
                    statuscode: jqXHR.status,
                    status:     status
                }
            ]);
        });
        return true;
    }
});

我更改为 sendRequest - 对我有用:

content.js

chrome.extension.sendRequest({contentScriptQuery: 'login'}, function(messageResponse) {
            const[response, error] = messageResponse;
            if(response === null){
                console.log(error);
            } else {
                console.log(response.resData);
            }
});

background.js

chrome.extension.onRequest.addListener(function (message, sender, sendResponse) {
        if(message.contentScriptQuery === 'login') {
            $.ajax({
                type: 'POST',
                url:  'http://127.0.0.1:8080/api/v1/login/local',
            }).then(function(resData, status, jqXHR) {
                sendResponse(sendResponse([
                    {
                        resData: resData,
                        status:  status
                    }, null
                ]));
            }, function(jqXHR, status) {
                    sendResponse([
                        null, {
                            status: status
                        }
                    ]);
            });

            return true;
        }
    });