angular - 加载资源失败:服务器响应状态为 400 () 错误:Angular asp.net core 5 api
问题描述
我正在开发一个前端使用 Angular 12 和 API 中使用 ASP.net core 5 的应用程序。调用 HttpPost API 时出现错误加载资源失败:服务器响应状态为 400 ()
如果我使用字符串或整数参数,则没有问题,并且 API 被正确调用。但只有当我使用实体类传递值时,我才会收到此错误。
这是我在 asp.net 核心中的实体类
public class FrontEnd
{
public class SendOTPReq
{
public string MobileNo { get; set; }
public string Email { get; set; }
public int Type { get; set; }
}
}
这是API
[Route("SendOTP")]
[HttpPost]
public async Task<object> SendOTP(SendOTPReq otpReq)
{
try
{
CommonApiResponse commonResponse = new CommonApiResponse();
if (!ModelState.IsValid)
{
return new APIResponse(406, "", ModelState.Select(t => new { t.Key, t.Value.Errors[0].ErrorMessage }));
}
var Result = await _IFrontEndRepository.SendOTP(otpReq);
if (Result != null)
{
commonResponse.Status = "Success";
commonResponse.ErrorMsg = "";
commonResponse.ResultData = Result;
return new APIResponse(200, "Request Success", commonResponse);
}
else
{
commonResponse.Status = "Failed";
commonResponse.ErrorMsg = Result.Message;
commonResponse.ResultData = Result;
return new APIResponse(204, "Request Failed", commonResponse);
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
}
这是我的角度 component.ts 代码:
const otpReq = new SendOTPReq();
otpReq.MobileNo = this.registerForm.controls['phoneNo'].value;
otpReq.Email = this.registerForm.controls['email'].value;
otpReq.Type = 2; //2 is or email and 1 is for sms
this.accountsService.sendOTP(otpReq).subscribe(
(data) => {
//do stuff here
});
角服务方法:
export class AccountsService {
constructor(private httpClient: HttpClient,
private _router: Router) { }
sendOTP(otpReq: SendOTPReq): Observable<SendOTPReq> {
debugger;
return this.httpClient.post<SendOTPReq>(environment.ApiUrl + 'FrontEnd/SendOTP', otpReq)
.pipe(
catchError(this.errorHandler),
);
}
errorHandler(error: { error: { message: string; }; status: undefined; message: any; }) {
debugger;
let errorMessage = '';
if (error.error instanceof ErrorEvent) {
// Get client-side error
errorMessage = error.error.message;
} else {
// Get server-side error
if(error.status == undefined)
{
errorMessage = "The server connection with the application is showing error. Close the application and restart again."
}else{
errorMessage = `Error Code: ${error.status}\nMessage: ${error.message}`;
}
}
return throwError(errorMessage);
}
}
角模型类:
export class SendOTPReq
{
MobileNo!:string;
Email!: string;
Type!: number;
}
可能出了什么问题?
解决方案
您的 POST 请求的返回类型错误。object
当您的 Angular httpClient 要求返回时,您不能让控制器SendOTPReq
返回。
所以你需要
return this.httpClient.post<MyActualReturnType>(environment.ApiUrl + 'FrontEnd/SendOTP', otpReq)
推荐阅读
- linux - 我们是否需要在 UBIFS 中同步父目录以进行原子*和*持久文件更新
- angular - angular 6 - @Inject(DOCUMENT) build scrollTop 在 iphone chrome、safari 中不起作用
- java - Elasticsearch 在显示 t: failed to read local state 后崩溃,正在退出
- java - Quartz 调度程序在 tomcat 上运行作业 48 小时而不是 24 小时
- javascript - 为什么我的点击功能在移动设备上的这个 SVG 中不起作用,但在桌面上却起作用?
- python - 在 python 中导入 Stocker 时中断
- c - Get the socket from an Apache module
- sql - 来自数据库的用户登录
- html - 什么机制阻止 div 完全占据其父级的宽度?
- mysql - 为什么“如果存在 konami 则删除程序”在 mysql 中不起作用?