0

What's the problem in the code.why it showing failed to fetch.tried all ways.please help.

async function searchJobs(searchTerm) { try { const response = await fetch(`https://jobs.github.com/positions.json?description=${encodeURIComponent(searchTerm)}`); if (!response.ok) { throw new Error(`Failed to fetch: ${response.status} ${response.statusText}`); } const data = await response.json(); displayResults(data); } catch (error) { console.error('Error fetching data:', error.message); resultsDiv.innerHTML = `<p>Error fetching data: ${error.message}</p>`; } } function displayResults(jobs) { resultsDiv.innerHTML = ''; if (jobs.length === 0) { resultsDiv.innerHTML = '<p>No jobs found.</p>'; } else { jobs.forEach(job => { const jobElement = document.createElement('div'); jobElement.classList.add('job');

19th Apr 2024, 4:30 PM
Vidhya Tharan
Vidhya Tharan - avatar
5 Respostas
+ 1
This kind of error usually occurs when the fetch function encounters a problem while trying to retrieve data from a specified URL. When the server hosting the data restricts access from web pages hosted on different domains for security reasons ( For both Sololearn and Github ). We call it CORS or Cross-Origin Resource. This is a security feature. This restriction helps prevent malicious websites from accessing sensitive data from other sites without permission. But in your case I think you use Invalid URL. Check that URL it won't work! Solutions are many to resolve this issue e.g. - Server Side Configuration, - Proxy Server, - JSONP (JASON with padding) or - CORS Proxy Server Read more: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS But tell me first is that you the owner of that repo? If not then try to create your own JSON file.
20th Apr 2024, 2:46 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 3
Take a look at this tutorial how to attached code with your question in Q&A Discussion section which is very helpful for the community to solve your problem⤵️ https://sololearn.com/compiler-playground/Wek0V1MyIR2r/?ref=app
19th Apr 2024, 5:38 PM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar
+ 1
your code is incomplete. link it instead of pasting it into the description.
19th Apr 2024, 4:35 PM
Lisa
Lisa - avatar
+ 1
It not mine. I'll make my own json file and update to you
20th Apr 2024, 7:30 AM
Vidhya Tharan
Vidhya Tharan - avatar
+ 1
Sure
20th Apr 2024, 7:30 AM
`ᴴᵗᵗየ
`ᴴᵗᵗየ - avatar