Ajax成功函数不会执行

时间:2017-02-08 20:46:26

标签: c# jquery ajax cordova http

我有一个apache cordova项目,我试图将表单数据从客户端发布到数据库。我可以看到我发送的json发送到我的API,然后将数据发送到DAL然后发送到数据库。该条目正确更新表,然后根据是否存在先前条目返回1或-1。然后将该值发送给客户端。我可以看到发回的确切响应,但我的ajax成功功能不会被激发。

的Ajax:

    $("#submit").click(function () {
        var info = {
            Username: $("#username").val(),
            Password: $("#password").val(),
            Firstname: $("#firstname").val(),
            Lastname: $("#lastname").val(),
            Email: $("#email").val()
        };

        info = JSON.stringify(info);

        event.preventDefault();
        $.ajax({
            type: "POST",
            dataType: "json",
            async: false,
            url: "http://localhost:57207/api/User/RegisterUser",
            data: info,
            success: function(data) {
                console.log(data);
            },
            error: function(input) {
                console.log(JSON.stringify(input));
            }
        })
    });

控制器:

    [System.Web.Http.HttpPost]
    public JObject RegisterUser(JObject obj)
    {
        RegisterUsers user = new RegisterUsers();
        user = JsonConvert.DeserializeObject<RegisterUsers>(((JProperty)obj.First).Name);
        var temp = DataAccessLayer.RegisterUser(user.Username, user.Password, user.Firstname, user.Lastname, user.Email, null, null);
        JObject jobj = new JObject();
        jobj.Add("output", temp.ToString());

        return jobj;
    }

我一直在使用fiddler检查我的回复和请求,这是原始数据:

请求:

POST http://localhost:57207/api/User/RegisterUser HTTP/1.1
Host: localhost:57207
Connection: keep-alive
Content-Length: 125
Accept: text/html, */*; q=0.01
Origin: http://evil.com/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36
Content-Type: application/x-www-form-urlencoded; charset=UTF-8
Referer: http://localhost:4400/test.html
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

{"Username":"johndoe","Password":"password!!!","Firstname":"john","Lastname":"doe","Email":"johndoe@random.com"}

响应:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
Access-Control-Allow-Origin: *
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcRGV2ZWxvcG1lbnRcVGV4dDJQaG9uaWNzXFJlc3RBUElcYXBpXFVzZXJcUmVnaXN0ZXJVc2Vy?=
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,POST,OPTIONS
Access-Control-Allow-Headers: Content-Type, soapaction
Date: Wed, 08 Feb 2017 19:52:43 GMT
Content-Length: 15

{"output":"-1"}

我的错误函数是触发的函数,这是输出:

的console.log:

{"readyState":0,"status":0,"statusText":"NetworkError: Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://localhost:57207/api/User/RegisterUser'."}

更新

我能够将async设置为true,我的新错误信息就是:

{"readyState":0,"status":0,"statusText":"error"}

1 个答案:

答案 0 :(得分:0)

请尝试返回IHttpActionResultIActionResultOk()以获得200回复​​。

public IActionResult RegisterUser(JObject obj)
{
    RegisterUsers user = new RegisterUsers();
    user = JsonConvert.DeserializeObject<RegisterUsers>(((JProperty)obj.First).Name);
    var temp = DataAccessLayer.RegisterUser(user.Username, user.Password, user.Firstname, user.Lastname, user.Email, null, null);
    JObject jobj = new JObject();
    jobj.Add("output", temp.ToString());

    return Ok(jobj);
}

如果它仍然失败,请尝试返回一个简单的字符串,如return Ok("Hello World!");。如果它使用简单的字符串成功,那么对象的序列化可能会失败或者作为无效的JSON出现。