我正在尝试使用跨域的ajax POST,PUT和删除方法实现一个页面。我在从服务器向客户端返回响应消息时遇到问题。
如果控制器返回 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 的候选者已被删除,但我收到错误
启用跨域而不是使用 jsonp 的更好方法
有关更多详细信息:在此处输入链接说明