HTTP Post响应导致Javascript错误

时间:2017-09-17 11:38:33

标签: javascript json ajax http post

我正在从浏览器向我的服务执行POST请求,该请求成功使用状态代码201 Created。执行此操作的应用程序片段如下。我可以在数据库中看到已成功存储已触发的JSON对象。

问题是AJAX调用永远不会进入done()部分,但总是会在fail()部分结束,即使资源已创建。

触发AJAX的JavaScript

$(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(XMLHttpRequest, textStatus, errorThrown) {
            //alert("Fail!");
            console.log(XMLHttpRequest, textStatus, errorThrown);
        });
    });
});

正在发送JSON

{
"FirstName": "Tia",
"LastName": "Tuon",
"BirthPlace": "London",
"Gender": "F",
"OIB": "19355864391",
"CurrentPlace": "Paris",
"Department": "21570"
}

使用Postman测试服务响应

access-control-allow-origin →*
cache-control →no-cache
connection →Keep-Alive
content-length →152
content-type →application/json; charset=utf-8
date →Sun, 17 Sep 2017 11:27:39 GMT
expires →-1
keep-alive →timeout=15, max=99
location →http://127.0.0.1:8080/api/employee/45
pragma →no-cache
server →Mono.WebServer.XSP/4.4.0.0 MacOSX
x-aspnet-version →4.0.30319

我正在尝试记录错误,但输出非常有限:

{readyState: 0, getResponseHeader: ƒ, getAllResponseHeaders: ƒ, setRequestHeader: ƒ, overrideMimeType: ƒ, …} "error" ""

为什么我总是最终进入AJAX通话的fail()部分?

0 个答案:

没有答案