Post Request 在 Postman 中工作,但在 Angular 2 应用程序中返回 Preflight 错误

编程入门 行业动态 更新时间:2024-10-24 20:13:56
本文介绍了Post Request 在 Postman 中工作,但在 Angular 2 应用程序中返回 Preflight 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这是来自浏览器控制台的错误日志XMLHttpRequest 无法加载 domain/xx/xxxxxxxx.预检响应具有无效的 HTTP 状态代码 404

THis is the error log from browser console XMLHttpRequest cannot load domain/xx/xxxxxxxx. Response for preflight has invalid HTTP status code 404

这是在 Postman 中收到的预期响应

This is the expected response as received in Postman

{ "status": "success", "code": "E012", "message": "Contact sent" }

这是来自Angular 2的请求

this is the request made from Angular 2

makeRequest(name, recipient) { let body = JSON.stringify({ "name": name, "recipient": recipient }); let authToken = localStorage.getItem('token'); console.log(authToken); let headers = new Headers({'Authorization': authToken , 'Content-Type': 'application/json'}); let options = new RequestOptions({headers: headers}); return this.http.post(url, body, options ) .map(res => res.json());

}

这里是 Yii2 中的行为函数

Here is the behaviors function in Yii2

public function behaviors() { $behaviors = parent::behaviors(); // remove authentication filter $auth = $behaviors['authenticator']; unset($behaviors['authenticator']); // add CORS filter $behaviors['corsFilter'] = [ 'class' => Cors::className(), 'cors' => [ // restrict access to 'Origin' => ['*'], 'Access-Control-Request-Method' => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'], // Allow only POST and PUT methods 'Access-Control-Request-Headers' => ['*'], // Allow only headers 'X-Wsse' 'Access-Control-Allow-Credentials' => true, // Allow OPTIONS caching 'Access-Control-Max-Age' => 86400, // Allow the X-Pagination-Current-Page header to be exposed to the browser. 'Access-Control-Expose-Headers' => [], ], ]; // re-add authentication filter $behaviors['authenticator'] = $auth; // avoid authentication on CORS-pre-flight requests (HTTP OPTIONS method) $behaviors['authenticator']['except'] = ['options']; return $behaviors; }

这是我的 UrlManager

Here is my UrlManager

'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => true, 'showScriptName' => false, 'rules' => [ 'POST <version:[w-]+>/sms' => '<version>/sms/send', 'POST <version:[w-]+>/users/verify' => '<version>/user/verify', 'POST <version:[w-]+>/bulk' => '<version>/routine/index', 'POST <version:[w-]+>/contact' => '<version>/contact/index'],

推荐答案

在某些情况下,浏览器会自动执行 预检请求 在实际发送卷轴之前检查允许的方法或动词列表.您可以在浏览器的网络选项卡中看到这些内容.我猜在 Postman 中,您是直接发送 POST 请求,而预先发送的 OPTIONS 请求应该是失败的请求.

In certain cases a browser will automatically perform a preflight request to check the list of allowed methods or verbs before actually sending the reel one. You can see those within your browser's network tab. I guess in Postman you are directly sending the POST request while the pre-sent OPTIONS request should be the failing one.

Yii 有一个内置动作在 ActiveController 类下定义 响应此类请求.但是在您的情况下,您是直接扩展其父控制器,因此您需要在控制器中手动定义类似的操作 (或它们的父控制器) 以响应预检请求:

Yii has a built-in action which is defined under the ActiveController class to respond to such requests. But in your case you are directly extending its parent controller instead so you'll need to manually define a similar action inside your controllers (or a parent one to them) responding to preflight requests:

public function actionOptions() { if (Yii::$app->getRequest()->getMethod() !== 'OPTIONS') { Yii::$app->getResponse()->setStatusCode(405); } $allowed_verbs = ['GET', 'POST', 'HEAD', 'OPTIONS']; Yii::$app->getResponse()->getHeaders()->set('Allow', implode(', ', $allowed_verbs)); }

还有;因为您没有使用 内置的 REST 路由机制;在您的情况下,您还需要手动定义 rules 到该 Options 操作:(从您的评论中编辑的代码版本)

Also; as you are not using the built-in routing mechanism for REST; in your case you'll also need to manually define rules to that Options action: (edited version of the code from your comments)

'urlManager' => [ 'enablePrettyUrl' => true, 'enableStrictParsing' => true, 'showScriptName' => false, 'rules' => [ 'POST <version:[w-]+>/users/verify' => '<version>/user/verify', 'POST <version:[w-]+>/airtime' => '<version>/airtime/airtime', 'POST <version:[w-]+>/bulk' => '<version>/routine/index', 'POST <version:[w-]+>/contact' => '<version>/contact/index', // OPTTIONS URI ENDPOINTS 'OPTIONS <version:[w-]+>/users/verify' => '<version>/user/options', 'OPTIONS <version:[w-]+>/airtime' => '<version>/airtime/options', 'OPTIONS <version:[w-]+>/bulk' => '<version>/routine/options', 'OPTIONS <version:[w-]+>/contact' => '<version>/contact/options', ], ];

更多推荐

Post Request 在 Postman 中工作,但在 Angular 2 应用程序中返回 Preflight 错误

本文发布于:2023-10-29 07:54:34,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539141.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:但在   应用程序   错误   工作   Request

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!