如何使用跨域修复 ajax post 方法和 asp.net api 中的 'A callback parameter was not provided in the request URI.' 错误
How to fix 'A callback parameter was not provided in the request URI.' error in ajax post method and asp.net api using cross domain
我正在尝试使用 ajax POST、跨域的 PUT 和删除方法来实现一个页面。我在 return 从服务器向客户端发送响应消息时遇到问题。
如果控制器 return httpStausCode 没有任何附加消息没有出现错误但是有一些附加消息我在服务器端收到错误 'A callback parameter was not provided in the request 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,消息 Candidate with id = xx has been deleted 但我收到一个错误
启用跨域而不是 jsonp 的更好方法
我正在尝试使用 ajax POST、跨域的 PUT 和删除方法来实现一个页面。我在 return 从服务器向客户端发送响应消息时遇到问题。
如果控制器 return httpStausCode 没有任何附加消息没有出现错误但是有一些附加消息我在服务器端收到错误 'A callback parameter was not provided in the request 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,消息 Candidate with id = xx has been deleted 但我收到一个错误
启用跨域而不是 jsonp 的更好方法