我用find
来获取。这几天工作正常,但现在它无法正常工作。我使用mlab
存储数据。当邮递员提出要求时我会变得如下。
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
在客户端调用API时,它显示错误
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.
哪里出错了?
答案 0 :(得分:1)
当您从客户端调用api时,您正在XMLHttpRequest
访问与您的网页不同的域。因此浏览器会阻止它,因为出于安全原因,它通常允许同一来源的请求。当您想要执行跨域请求时,您需要做一些不同的事情。请添加以下中间件以解决此问题。
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();
});