[英]Convert Curl request to Angular request
I have a problem with this CURL request.这个 CURL 请求有问题。 It works fine if I send it over terminal but when I convert it to Angular HTTP Client call it doesn't work.
如果我通过终端发送它,它工作正常,但是当我将它转换为 Angular HTTP 客户端调用时,它不起作用。
Curl:卷曲:
curl --location --request PUT '<some_url>' \
--header 'Accept: application/json' \
--header 'Content-Type: application/json' \
--data-raw '{
"status": "CONFIRMED"
}'
Angular:角度:
const headers = new HttpHeaders()
.set('Content-Type', 'application/json')
.set('Accept', 'application/json; charset=UTF-8');
const b = {
status: 'CONFIRMED'
};
return this.http.put(some_url, b, { headers });
Error: Access to <some_url> from origin 'http://localhost:4200' has been blocked by CORS policy: Method PUT is not allowed by Access-Control-Allow-Methods in preflight response.错误:从源“http://localhost:4200”访问 <some_url> 已被 CORS 策略阻止:预检响应中的 Access-Control-Allow-Methods 不允许方法 PUT。
Try this code :试试这个代码:
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Accept', 'application/json; charset=UTF-8'
}),
};
const b = {
status: 'CONFIRMED'
};
return this.http<any>(some_url, b,httpOptions)
.pipe(catchError(this.errorHandler));
use a proxy.config.json file使用 proxy.config.json 文件
{
"/api/*": {
"target": "http://backendurl",
"secure": false,
"changeOrigin": true
}
}
add the proxy configuration the angular.json under the serve->options :在 serve->options 下添加代理配置 angular.json :
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"options": {
"browserTarget": "your-application-name:build",
"proxyConfig": "src/proxy.conf.json" // add this ligne
},
return this.http.put(some_url, body);
应该管用。
确保在put()
subscribe()
之后调用subscribe()
,否则根本不会执行请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.