Reputation: 425
I used find
to get. This worked fine for few days but now it's not working properly. I'm using mlab
to store data. I'm getting like below when requested in postman.
Could not get any response
There was an error connecting to http://localhost:3060/subproduct.
Why this might have happened:
The server couldn't send a response:
Ensure that the backend is working properly
SSL connections are being blocked:
Fix this by importing SSL certificates in Chrome
Cookies not being sent:
Use the Postman Interceptor extension
Request timeout:
Change request timeout in Settings > General
When API is called on client side, it is showing error as
XMLHttpRequest cannot load. No 'Access-Control-Allow-Origin' header is present on the requested
resource. Origin is therefore not allowed access. The response had HTTP status code 503.
Where it went wrong?
Upvotes: 0
Views: 87
Reputation: 7964
When you call the api from client side you are doing an XMLHttpRequest
to a different domain than your page is on. So the browser is blocking it as it usually allows a request in the same origin for security reasons. You need to do something different when you want to do a cross-domain request.Please add the following middle ware for fixing this issue.
var app = express();
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
Upvotes: 1