我正在尝试使用带有跨域的Ajax POST,PUT和delete方法来实现页面。从服务器返回响应消息到客户端时出现问题。
如果控制器返回httpStausCode却没有任何其他消息,则不会出现错误,但有一些其他消息,我在服务器端收到错误消息:“请求URI中未提供回调参数。”
请求代码
$("#myTable").on('click', '.deleteCandidate', function (e) {
e.preventDefault();
$.ajax({
type: "DELETE",
url: 'http://localhost:59838/api/candidate/' + $(this).data("id"),
content: "application/json; charset=utf-8",
crossDomain: true,
dataType: "json",
success: function (data, textStatus, jqXHR) {
console.log(data);
console.log(textStatus);
console.log(jqXHR);
},
error: function (xhr, textStatus, error) {
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}
});
});
服务器代码
public HttpResponseMessage Delete(int id)
{
try
{
using (MutliTiersDataBaseEntities entities = new MutliTiersDataBaseEntities())
{
var entity = entities.Candidates.FirstOrDefault(e => e.Id == id);
if (entity != null)
{
entities.Candidates.Remove(entity);
entities.SaveChanges();
return Request.CreateResponse(HttpStatusCode.OK, "Candidate with id = "+ id.ToString() +" has been deleted");
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Candidate with Id:" + id.ToString() + " not exist");
}
}
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}
我希望输出的状态码为200,ID为xx的候选消息已删除,但我遇到了错误