0

I had to replace my ajax query when migrating from manifest v2 to v3 because apparently external I cannot use JQuery in background page. so I replaced below function:

function ajaxCall(type, path, data, datatype, callback){
        $.ajax({
            url: domain+path,
            type: type,
            data: data,
            dataType: datatype, //"json" added to make sure the request not returning HTML
            success: function(response/*,textStatus,xhr*/){
                
                
                callback(response/*,xhr*/);
            },
            error: function(response/*,xhr*/){
                
                callback(false);
            }
        });
    }

With:

async function ajaxCall(type,path , data , datatype, callback) {
        url = domain+path;

        const form_data = new URLSearchParams(data);
        if (type === "GET"){
            options = {
                method: type
            }
        }
        else {
            options = {
                method: type, // *GET, POST, PUT, DELETE, etc.
                mode: 'cors', // no-cors, *cors, same-origin
                cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
                headers: {
                    // 'Content-Type': 'application/json'
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: form_data // body data type must match "Content-Type" header
            }
        }
        await fetch(url, options)
            .then((response) => {
            if (!response.ok) {
                console.log(response);
                throw new Error(response);
            }
            try {
                return response.json();
            }
            catch{
                throw new Error(response);
            }
            })
            .then((response) => {
                console.log('response: ', response);
                callback(response);
            })
            .catch((error) => {
                console.log('There has been a problem with your fetch operation', error);
                callback(false);
            });
    }

My extension works in a way that if user is already logged into to my website. He is no longer required to log into extension again and when user clicks on extension it logs user automatically in and fetches user info using OAuth. Below is the network tab when I use ajax: Network tab with ajax

Network tab with Fetch: network tab with Fetch

I suspect that fetch or in Manifest it does not forward cookie and thus it again returns login page. Can you help me how to get around this?

Update: After setting credentials: "include" it worked but after sometime. I guess when session gets timed out in Chrome extension backend it does not work. Here is the updated picture: after allowing cookies

Here I can see cookies being sent in user which redirects to login and then authorize but it gets stuck there. Can you help please?

John
  • 33
  • 10
  • 1
    Have you tried just setting `credentials: "include"` in your fetch options? – Wiktor Zychla Apr 12 '23 at 09:09
  • I tried as you said and it worked. Thanks a lot – John Apr 12 '23 at 09:22
  • @WiktorZychla I just noticied that it stopped working. I mean I need to refresh the main page of the website to make it work. Can you please help me? If you reopen the topic I can upload more photos – John Apr 12 '23 at 14:07

0 Answers0