cam*_*024 6 http dart flutter flutter-web dio
我正在package:dioFlutter Web 应用程序中使用。
然而,每当我发送 POST 请求时,它就会更改为 OPTIONS 请求。发出 API 请求的函数如下所示:
Future<LoginResponse> login(LoginRequest request) async {
final dio = Dio(BaseOptions(baseUrl: "http://localhost:8000"));
final response = await dio.post("/login", data: request.toJson());
return LoginResponse.fromJson(jsonDecode(response.data));
}
Run Code Online (Sandbox Code Playgroud)
此代码将OPTIONS请求发送到http://localhost:8000/login. 如果我将该端点添加到我的服务器,它就可以工作。
如果我手动从邮递员发送 POST 请求,它也可以工作。
如果我将此代码更改为其他方法(例如dio.delete(...)),它也会映射到OPTIONS请求。
为什么要dio重写我的请求?
我在向 Sanic 网络服务器发送 POST 请求时遇到了类似的问题。
@cross_origin()解决方案是从包中添加装饰器sanic_cors。
例子:
from sanic_cors import cross_origin
from sanic import Sanic
from sanic.response import json
from sanic.blueprints import Blueprint
app = Sanic(__name__)
endpoint = Blueprint('Test', '/test')
app.blueprint(endpoint)
@endpoint.route("/", methods=['POST', 'OPTIONS'])
@cross_origin(endpoint)
async def schranktyp(request):
return json({'message': 'OK'})
def main():
app.run(host="0.0.0.0", port=8000, debug=True)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
也许这会对将来的某人有所帮助。
| 归档时间: |
|
| 查看次数: |
4188 次 |
| 最近记录: |