r/learnprogramming • u/ZimmerFrameThief • Dec 11 '24
Debugging Need help with a failing fetch request
NB: I'm doing it this way for a reason - I'm sure there are other/better ways, but circumstance dictates I go about it this way - just don't want to get you all stuck in the weeds around the why.
Effectively, I have a little landing page that I am using for a handful of buttons that fire-off fetch request re: using webhooks.
What I am trying to achieve is, high-level, a user taps a button and that triggers a webhook, but without redirecting the user anywhere or opening a blank page/tab - the button should just work like a trigger/toggle.
I've tried (I think) the various combinations or HTTP and HTTPS, reverse proxy, portforward, POST vs GET, no-cors etc.
I know the webhook, link, server etc are all working as I can use the webhook link just in a browser - hit ender - and it triggers without a problem, so it's beyond me why it's so hard to achieve the same thing at button push.
I get various errors (depending on which iteration I've tried re: above) but the most common (from browser console) include
GET request failed: TypeError: Failed to fetch
at HTMLButtonElement.<anonymous> (<anonymous>:7:9)
VM2330:7 Mixed Content: The page at 'https://HOSTING-URL' was loaded over HTTPS, but requested an insecure resource 'WEBHOOK-URL'. This request has been blocked; the content must be served over HTTPS.
Hopefully what I'm trying to do is possible! and simply, with a little tweak, but if not - just for FYI - the webhook url is https, as is the site the button will be on. The Mixed content error is just a result of my trying to find the right combination (as above), so it pops up whenever I use a miss-matched HTTP/S combination.
<!DOCTYPE html>
<html>
<head>
<title>Double-Confirm Button</title>
<style>
#confirm-button {
background-color: #333;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
}
#confirm-button.active {
background-color: red;
font-size: 1.3em;
}
</style>
</head>
<body>
<button id="confirm-button">TEST</button>
<script>
const button = document.getElementById('confirm-button');
let isConfirmed = false;
button.addEventListener('click', () => {
if (isConfirmed) {
fetch('http://WEBHOOK-URL', {
mode: 'no-cors', method: 'POST'
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.text();
})
.then(data => {
console.log('GET request successful:', data);
})
.catch(error => {
console.error('GET request failed:', error);
alert('Error fetching data. Please try again later.'); // Display error message to the user
});
// Reset button state
button.classList.remove('active');
button.textContent = 'TEST';
isConfirmed = false;
} else {
// Set the confirmed state and change button appearance
button.classList.add('active');
button.textContent = 'Tap to Confirm & Unlock';
isConfirmed = true;
}
});
</script>
</body>
</html>
View all comments
Show parent comment
1
u/ZimmerFrameThief Dec 11 '24
Yeah, using edge, so when just using the address bar, I get 'ERR_SSL_PROTOCOL_ERROR'
It might be work holding off for a few hours - then I'll be able to test the SSL webhook the proper receiving server - as if it's just the mismatch, then it should just work (famous last words) as they're should be an SSL problem. I. Which case, ill come and close this post off. Else, I'll be back later still to pester you fine folk again!