angular 6 post 使用 `httpClient` 方法的**数组数据**不适用于 asp.net 核心 API
angular 6 post **array data** using `httpClient` method is not working with asp.net core API
我是 dot-net 和 angular 技术的新手,正在尝试 post 使用 angular 的 数组 数据-6 httpClient
方法,但我未能命中 HTTP Post API 方法(通过使用断点找到)。请帮我找出错误所在。
组件方法调用服务
let selectedLanguageList: userChoosenQC[] = [];
this.quizzerService.InsertUserChoosenDetails(selectedLanguageList)
.subscribe((data) => {});
服务class方法调用API
InsertUserChoosenDetails(data: userChoosenQC[]): any {
const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
var details = this.httpClient.post<userChoosenQC[]>(this.apiUrl + '/Questions/InsertUserLikes',
data, httpOptions);
return details;}
userChoosenQC
接口类型
export interface userChoosenQC{
Id :number
QuizType :number;
QuizzerDetails :number
QuizQuestions:number;
QuizChoices :number;
}
我未能点击此 HTTP-POST API 方法
[HttpPost, Route("InsertUserLikes")]
public ActionResult InsertUserSelections(List<QuizzerQCdetails> userSelectedQC)
{
userRepository.InsertUserSelectedQC(userSelectedQC);
return Ok(new { res="data Inserted sucessfully" });
}``
QuizzerQCdetails
POCO class
public class QuizzerQCdetails
{
public int Id { get; set; }
public QuizType quizType { get; set; }
public QuizzerDetails quizzerDetails { get; set; }
public QuizQuestions quizQuestions { get; set; }
public QuizChoices quizChoices { get; set; }
}
我遇到了这个错误
HTTP/1.1 405 Method Not Allowed"
这个错误,好像是cors问题导致的。尝试在 Web api 中配置 CORS,例如
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.WithOrigins("http://example.com")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
和
app.UseCors();
另外,不确定你当前的Controller代码,尝试添加FromBody
[HttpPost, Route("InsertUserLikes")]
public ActionResult InsertUserSelections([FromBody]List<QuizzerQCdetails> userSelectedQC)
我是 dot-net 和 angular 技术的新手,正在尝试 post 使用 angular 的 数组 数据-6 httpClient
方法,但我未能命中 HTTP Post API 方法(通过使用断点找到)。请帮我找出错误所在。
组件方法调用服务
let selectedLanguageList: userChoosenQC[] = [];
this.quizzerService.InsertUserChoosenDetails(selectedLanguageList)
.subscribe((data) => {});
服务class方法调用API
InsertUserChoosenDetails(data: userChoosenQC[]): any {
const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json' }) };
var details = this.httpClient.post<userChoosenQC[]>(this.apiUrl + '/Questions/InsertUserLikes',
data, httpOptions);
return details;}
userChoosenQC
接口类型
export interface userChoosenQC{
Id :number
QuizType :number;
QuizzerDetails :number
QuizQuestions:number;
QuizChoices :number;
}
我未能点击此 HTTP-POST API 方法
[HttpPost, Route("InsertUserLikes")]
public ActionResult InsertUserSelections(List<QuizzerQCdetails> userSelectedQC)
{
userRepository.InsertUserSelectedQC(userSelectedQC);
return Ok(new { res="data Inserted sucessfully" });
}``
QuizzerQCdetails
POCO class
public class QuizzerQCdetails
{
public int Id { get; set; }
public QuizType quizType { get; set; }
public QuizzerDetails quizzerDetails { get; set; }
public QuizQuestions quizQuestions { get; set; }
public QuizChoices quizChoices { get; set; }
}
我遇到了这个错误
HTTP/1.1 405 Method Not Allowed"
这个错误,好像是cors问题导致的。尝试在 Web api 中配置 CORS,例如
services.AddCors(options =>
{
options.AddPolicy("CorsPolicy",
builder => builder.WithOrigins("http://example.com")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials());
});
和
app.UseCors();
另外,不确定你当前的Controller代码,尝试添加FromBody
[HttpPost, Route("InsertUserLikes")]
public ActionResult InsertUserSelections([FromBody]List<QuizzerQCdetails> userSelectedQC)