【发布时间】:2017-09-17 03:00:45
【问题描述】:
我正在使用 Ajax 向我的 web api 发送一个请求,用 c# 编写。 我正在发送一个视图模型,但我收到一个错误的请求错误。 这是我的代码:
[HttpPut]
/*api controller*/
public IHttpActionResult UpdateCursoProfesor(int CourseId, CourseViewModel ViewModel)
{
/*Do something*/
return Ok(result);
}
这是我的视图模型:
public class CursoViewModel
{
[MaxLength(125)]
public string Tittle{ get; set; }
public string Description { get; set; }
}
这是我的 ajax
function GeneralPost(CursoDto) {
$.ajax({
async: true,
url: "http://localhost:59245/api/ProfesorCurso?CourseId=123",
type: "Put",
dataType:'json',
data: JSON.stringify({
Tittle: 'My tittle',
Description: 'I am describing yea'
}),
success: function (data) {
alert ('we did it!')
},
error: function (msg) { alert('bad bad'); }
});
}
当我在 ajax 请求中不包含“数据”时,我可以到达我的 api 控制器,但是当我包含数据时,出现异常: http://localhost:59245/api/ProfesorCurso?CourseId=400 错误请求
关于我做错了什么的任何提示?谢谢
【问题讨论】:
-
检查请求标头,特别是 Content-Type。当标头与内容格式不匹配时,通常会返回 Bad Request。
标签: c# jquery ajax asp.net-mvc