Currently I am trying to make a post request for my chrome extension and I keep getting Fetch Failed
as the response in the console.
popup.html
<!DOCTYPE html>
<html>
<head>
<title>Tube Hunt: Community Curated YouTube Recommendations</title>
<script src="popup.js"></script>
</head>
<body style="width: 15rem; height: 15rem">
<div class="container-fluid" style="padding: 10px">
<a
href="https://www.reddit.com/r/TubeHunt/"
target="_blank"
class="badge badge-secondary"
>/r/TubeHunt</a
>
<form
name="inputForm"
action="https://us-central1-tube-hunt.cloudfunctions.net/app/api/channel/submit"
method="POST"
>
<input
id="url"
value="https://www.youtube.com/c/PickUpLimes"
name="url"
/>
<button type="submit" id="newDetails">Submit</button>
</form>
<button type="button" id="getChannels">Get Channels</button>
</div>
</body>
</html>
popup.js
btn.addEventListener("click", function (e) {
e.preventDefault();
if (url.value) {
console.log(url.value);
let data = new FormData();
const myHeaders = new Headers({
Accept:"application/json, application/xml, text/plain, text/html
});
const baseURL ="myurl";
data.append("url", `${url.value}`);
fetch(baseURL, {
headers: myHeaders,
method: "POST",
body: data,
mode: 'cors'
})
.then((res) => {
console.log(res)
return res.text()
})
.then((html) => console.log(html))
.catch(function (err) {
console.log("Problem");
console.log(err);
});
}
});
As requested, here is the manifest.json
. As you can see, I added the permissions for my server here.
{
"manifest_version": 2,
"name": "Tube Hunt",
"version": "0.1",
"content_scripts": [
{
"matches": [
"https://www.youtube.com/*"
],
"js": [
"jquery.min.js",
"content.js",
"background.js"
]
}
],
"background": {
"scripts": [
"background.js"
]
},
"browser_action": {
"default_icon": "download.png",
"default_popup": "popup.html"
},
"permissions":[
"https://us-central1-tube-hunt.cloudfunctions.net/app/api/*"
]
}
At the moment, I am able to reach my server but am getting a 500
error code from it. I am able to perform a GET request successfully, just not at POST request. In addition to my research, I have also found this link for fetching which isn't helping much.