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:
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:
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?