奇怪的情况。我正在使用AJAX将一些数据发布到服务,并且成功 - 这意味着我可以真正看到存储在数据库中的新数据。但是,我的代码没有进入done()部分,但由于某种原因总是在fail()部分结束。我提供相关代码。
$(document).ready(function(){
// on-click listener
$("#btn-submit").click(function(e) {
// obtain data from the form
var employee = {
FirstName: $("#input-first-name").val(),
LastName: $("#input-last-name").val(),
BirthPlace: $("#input-birth-place").val(),
CurrentPlace: $("#input-current-place").val(),
Gender: $("#input-gender").val(),
Department: $("#input-department").val(),
OIB: $("#input-oib").val()
};
// AJAX call on button clicked
$.ajax({
type: "POST",
dataType: "json",
url: "http://127.0.0.1:8080/api/employee/",
data: employee
}).done(function(){
alert("Successfully created new entry!");
}).fail(function(xhr) {
//alert("Fail!");
console.log(xhr);
});
});
});
我正在尝试记录错误消息,但它只是这样:
{readyState: 0, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …}
服务器的响应生成如下。
[HttpPost]
[Route("")]
public HttpResponseMessage Post([FromBody]EmployeeDto employee)
{
long id = persistence.SaveEmployee(employee);
// create http response
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created);
response.Headers.Location = new Uri(Request.RequestUri, String.Format("/api/employee/{0}", id));
return response;
}
使用201 Created状态代码生成以下响应:
Date: Sat, 16 Sep 2017 11:11:51 GMT
Server: Mono.WebServer.XSP/4.4.0.0 MacOSX
Location: http://127.0.0.1:8080/api/employee/40
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
Content-Length: 153
Expires: -1
Pragma: no-cache
Cache-Control: no-cache
Content-Type: application/json; charset=utf-8
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
我真的不知道这意味着什么,也不知道为什么代码在实际设法发布新条目时会进入失败部分。有什么想法吗?