【问题标题】:asp.net razor pages webmatrix ajax post won't workasp.net razor pages webmatrix ajax post 不起作用
【发布时间】:2014-02-26 05:36:13
【问题描述】:

所以我只是想用 ajax 将数据发布到页面.. 但它不起作用.. 有什么想法吗?

                $.ajax({
                url: '/REST/GetResponse.cshtml',
                type: "POST",
                dataType: "json",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({
                    username: $('#username').val(),
                    firstname: $('#firstname').val(),
                    lastname: $('#lastname').val(),
                    email: $('#email').val(),
                    password1: $('#password1').val(),
                    password2: $('#password2').val(),
                }),
                success: function (result) {
                    alert("success " + result.UserName);
                },
                error: function (result) {
                    alert("Failed");
                }

            });
        }

这是我的 ajax 帖子 -

@{
if (IsPost)
{
    var password = Request["password1"];
    var username = Request["username"];
}

}

这是它被发布到的页面,但是当我查看控制台时它显示(已取消)并且错误功能会弹出警报。

感谢您的帮助 :) 谢谢

【问题讨论】:

  • 您似乎期待 POSTed 键/值对 (Request["key"]) 与 JSON.stringify(对象 {"x":5,"y":6} 的字符串表示)

标签: jquery asp.net ajax razor webmatrix


【解决方案1】:

ASP.Net 网页通常期望 POST 请求以 application/x-www-form-urlencoded 而不是 JSON 格式发送。只需将contentType 设置为那个(或者根本不设置它,因为这是默认设置)并摆脱JSON.stringify() 调用。 jQuery 会自动为你进行编码。

【讨论】:

    【解决方案2】:

    对您来说最好的选择是序列化您的表单并提交。如果您的表单 ID 是“userdata”,您的 Ajax 帖子应该是:

    $.ajax({
        url: '/REST/GetResponse.cshtml',
        type: "POST",
        dataType: "text",
        data: $("#userdata").serialize(),
        success: function (result) {
            alert("success " + result);
        },
        error: function () {
            alert("Failed");
        }
    };
    

    和您的 /REST/GetResponse.cshtml 文件类似:

    @{
        var password = Request["password1"];
        var username = Request["username"];
        // and so on ...
    
        try 
        {
            // do something...
            Response.Write(username);
        }
        catch (Exception ex)
        {
            Response.StatusCode = 500;
            Response.Write(ex.Message);
        }
    }
    


    相反,如果你想保留原来的 Ajax 帖子,我认为你的 /REST/GetResponse.cshtml 应该是:

    @{
        string input;
        using(var reader = new StreamReader(Request.InputStream)){
          input = reader.ReadToEnd();
        }
        var user = Json.Decode(input);
        var password = user.password1;
        var username = user.username;
        // and so on ...
    
        try 
        {
            // do something...
            Response.Write(username);
        }
        catch (Exception ex)
        {
            Response.StatusCode = 500;
            Response.Write(ex.Message);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-30
      • 2019-10-12
      • 1970-01-01
      • 2021-04-02
      • 1970-01-01
      • 1970-01-01
      • 2017-09-29
      • 1970-01-01
      • 2020-02-03
      相关资源
      最近更新 更多