Try to search your question here, if you can't find : Ask Any Question Now ?

How to solve a cross domain issue for fetching json data from a different url in javascript?

HomeCategory: stackoverflowHow to solve a cross domain issue for fetching json data from a different url in javascript?
Avatarwillson asked 4 months ago

I am trying to fetch JSON data from an URL but it’s giving me an error(Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://core.agricx.com/api/Apiagricx/get_config_test. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).

backend code(node js)

app.use(function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
    next();
});

Front end code(dashboard.js)

fetch('https://core.agricx.com/api/Apiagricx/get_config_test', {
    method: "GET",
    credentials: "include",
    cache: "no-cache"
})
.then(response => response.json())
.then(out => {
   console.log("for out");
   console.log(out);
})
.catch(err => {
   console.log("for err");
   console.error(err);
});

I expected the JSON data output which you can see on directly typing the URL. But the compiler is going to the catch block as it is blocking the response I am getting from that URL.

1 Answers
Best Answer
AvatarArben answered 4 months ago
Your Answer

15 + 10 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com