I am running a express server just for routing purpose and client page is using axios to get the url. I have tried every possible solution i could find during this whole day, without success.
- I understand if the request if not from the same domain, it is considered as CORS by the target server.
- The response is then sent with a header that the request was originally from another domain.
- The browser blocks the content and hence cannot be read as response through the response code block on the client page.
However, i can see the json page in the response by clicking on the response tab of the developer console.
Server Page
const express = require('express')
const cors = require('cors')
const app = express()
app.use(
cors({
credentials: true,
origin: [
'http://localhost:8080'
]
}),
)
app.options('*', cors());
app.use(express.static(__dirname + '/public'));
app.get('/', function (req, res) {
res.sendFile( __dirname + "/public/" + "index.html" );
})
Client page
var nse = axios.create();
// delete nse.defaults.headers.common["Access-Control-Allow-Origin"];
//nse.defaults.headers.common['Access-Control-Allow-Origin']='http://localhost:8081';
// nse.defaults.headers.common['Access-Control-Allow-Methods']='POST, OPTIONS';
// nse.defaults.headers.common['Access-Control-Max-Age']= 86400;
// nse.defaults.headers.common['Access-Control-Allow-Headers']= 'Origin, Content-Type, Accept';
nse.get('https://www1.nseindia.com/live_market/dynaContent/live_watch/stock_watch/liveIndexWatchData.json'
,{ crossdomain: true })
.then(resp => {
console.log("xxxxx");
data = resp.data.data;
window.alert("hi");
})
.catch(function (error) {
if (error.response) {
console.log(error.response.data);
console.log(error.response.status);
}
});
I am getting the following error in the developer console on the firefox browser.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www1.nseindia.com/live_market/dynaContent/live_watch/stock_watch/liveIndexWatchData.json. (Reason: CORS header ‘Access-Control-Allow-Origin’ missing).
I believe this one is tough nut to crack. Please help me to resolve this error so that i will be able to read the JSON response programatically.