Unable to make API Calls in Production
Good day everyone.
I just hosted my website and everything was going well but my scripts (.js files) couldn't make API calls. It's keeps giving me CROSS ORIGIN issues. Trust me, I have tried every possible means to rectify it on my own, but I couldn't. I even added ALLOW-CROSS-ORIGIN directive / tag in .htaccess of my server. Still, I couldn't get it to work.
I have tweaked everything tweakable, it still not working. Below is my .htaccess config
Below is one of my .js code
fetch("https://api.myip.com",{
mode: 'cors',
headers: {'Access-Control-Allow-Origin':'*'}
}).then(function (response) {
return response.json();
})
Error: `Access to fetch at 'https://api.myip.com/' from origin 'https://whatsappconnects.com' has been blocked by CORS policy: Response to preflight request doesn't 开发者_如何学Pythonpass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
GET https://api.myip.com/ net::ERR_FAILED Uncaught (in promise) TypeError: Failed to fetch at index.js:316:13`
Also, i have a tesseract ocr javascript code. That's not working too
Tesseract.recognize(url, "eng", {workerPath:"https://unpkg.com/tesseract.js@v4.0.0/dist/worker.min.js",
langPath:"https://tessdata.projectnaptha.com/4.0.0",
corePath:"https://unpkg.com/tesseract.js-core@v4.0.0/tesseract-core.wasm.js",
logger: function (m) { console.log(m.status) },
});
I experienced these issues during the development stage of the project, but I bypassed them by using Moesif CORS Chrome plugin. I thought the error would go away in production, but it became worse. I've been on this for a few days now. I seriously need help, Thanks. Also, I did a lot of research and tried almost all the techniques provided, but none worked.
I believe that the cors error occurs when the server not giving your page url to access it, not when you fetch it. This should be a comment but sorry I dont have enough reputation points :D
精彩评论