ajax成功或失败的原因,实际上成功后,AJAX发布请求报告失败
奇怪的情況。我正在使用AJAX將一些數(shù)據(jù)發(fā)布到服務(wù),并且成功 - 這意味著我可以真正看到存儲(chǔ)在數(shù)據(jù)庫(kù)中的新數(shù)據(jù)。但是,我的代碼沒有進(jìn)入done()部分,但由于某種原因總是在fail()部分結(jié)束。我提供相關(guān)代碼。
$(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);
});
});
});
我正在嘗試記錄錯(cuò)誤消息,但它只是這樣:
{readyState: 0, getResponseHeader: ?, getAllResponseHeaders: ?, setRequestHeader: ?, overrideMimeType: ?,?…}
服務(wù)器的響應(yīng)生成如下。
[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狀態(tài)代碼生成以下響應(yīng):
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
我真的不知道這意味著什么,也不知道為什么代碼在實(shí)際設(shè)法發(fā)布新條目時(shí)會(huì)進(jìn)入失敗部分。有什么想法嗎?
總結(jié)
以上是生活随笔為你收集整理的ajax成功或失败的原因,实际上成功后,AJAX发布请求报告失败的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 130塔式服务器系统,PowerEdge
- 下一篇: 华为手机服务器响应,服务器响应优化