所以我想知道是否可以以角度服务发送跨域请求?因为我有服务,但是当我发送一个请求时,我会得到以下错误:
所以我检查请求本身:
没有启用CORS,然后我尝试将其添加到后端,因为我得到了很多答案,但仍然没有帮助。我的代码是:
this.http.get(“https://www.linkedin.com/oauth/v2/accesstoken”?grant_type=client_credentials&client_id=[id]&client_secret=[secret]“).subscribe((data:any)=>console.log(data));
< /代码>
我想知道您能在GET请求中添加一个标题吗?如果需要更多的细节,只需写一条评论,我将在这里编辑。
编辑:
我的后端
var app=express();
应用程序使用((req,res,next)=>。{
res.append('access-control-allow-origin',['*']);
res.append('access-control-allow-methods'、'get、put、post、delete');
res.append(“access-control-allow-headers”,“content-type”);
(下);
(});
var mongoose=需要(“mongoose”);
mongoose.connect('mongo_url',usenewurlparser:true,promiselibrary:require('bluebird'))
。然后(()=>console.log(“连接成功”))
.catch((err)=>控制台。错误(err));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded(extended:false));
app.use(express.static(path.join(uu dirname,'dist/omrt'));
app.use(“/”,express.static(path.join(u dirname,'dist/omrt'));
app.use('/dashboard',express.static(path.join(u dirname,'dist/omrt'));
app.use(“/api”,apirouter);
//捕获404并转发到错误处理程序
应用程序使用(功能(req,res,next){
下一个(CreateError(404));
(});
//错误处理程序
应用程序使用(函数(err,req,res,next){
//设置局部变量,只提供开发中的错误
res.locals.message=err.message;
res.locals.error=req.app.get('env')=='开发'?错误:{};
//呈现错误页
Res.status(错误状态500);
res.sendstatus(错误状态);
(});
module.exports=应用程序;
< /代码>
我使用express/node.js后端,上面的代码片段我用来添加一个头来允许源代码。由于出现错误,我添加了本地主机。
重定向URL:
.
1答
所以我想知道是否可以以角度服务发送跨域请求?因为我有服务,但是当我发送一个请求时,我会得到以下错误:

所以我检查请求本身:

没有启用CORS,然后我尝试将其添加到后端,因为我得到了很多答案,但仍然没有帮助。我的代码是:
this.http.get("https://www.linkedin.com/oauth/v2/accessToken?grant_type=client_credentials&client_id=[ID]&client_secret=[SECRET]").subscribe((data:any) => console.log(data));
我想知道您能在GET请求中添加一个标题吗?如果需要更多的细节,只需写一条评论,我将在这里编辑。
编辑:
我的后端
var app = express();
app.use((req, res, next) => {
res.append('Access-Control-Allow-Origin', ['*']);
res.append('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.append('Access-Control-Allow-Headers', 'Content-Type');
next();
});
var mongoose = require('mongoose');
mongoose.connect('MONGO_URL', { useNewUrlParser: true, promiseLibrary: require('bluebird') })
.then(() => console.log('connection successful'))
.catch((err) => console.error(err));
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'dist/OMRT')));
app.use('/', express.static(path.join(__dirname, 'dist/OMRT')));
app.use('/dashboard', express.static(path.join(__dirname, 'dist/OMRT')));
app.use('/api', apiRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.sendStatus(err.status);
});
module.exports = app;
我使用express/node.js后端,上面的代码片段我用来添加一个头来允许源代码。因为这个错误,我添加了本地主机。
重定向URL:
