【问题标题】:How to receive a json string after a jquery post in WebMatrix?如何在 WebMatrix 中的 jquery 帖子后接收 json 字符串?
【发布时间】:2013-02-18 19:27:43
【问题描述】:

如果我有以下电话:

 $('#json_form').submit(function (event) {

            event.preventDefault();
            var url = $(this).attr('action');
            var datos = {
                "uno": "lalala",
                "dos": "jojojo"
            }
            var data = JSON.stringify(datos);

            $.post(url, data, function (resultado) {
                $('#posted_values').html(resultado);
            });
        });

如何接收和处理 cshtml 文件中的 json 对象?我的意思是我在解码调用中输入的内容:

if (IsPost)
    {
        var json_object = Json.Decode(Request???);
}

编辑完成@MikeBrind 的答案,以帮助其他有同样问题的人。 对更复杂的 json 对象使用 decode 的示例。

     $('#json_form').submit(function (event) {

            event.preventDefault();
            var url = $(this).attr('action');
            var datos = {
                "firstName": "John",
                "lastName": "Smith",
                "age": 25,
                "address": {
                    "streetAddress": "21 2nd Street",
                    "city": "New York",
                    "state": "NY",
                    "postalCode": 10021
                },
                "phoneNumber": [
                    {
                        "type": "home",
                        "number": "212 555-1234"
                    },
                    {
                        "type": "fax",
                        "number": "646 555-4567"
                    }
                ]
            }

            var data = JSON.stringify(datos);

            $.post(url, {"person": data}, function (resultado) {
                $('#posted_values').html(resultado);
            });
        });

接收和使用:

@{
    dynamic json_object;    
    if (IsPost)
    {
        json_object = Json.Decode(Request["person"]);
        @json_object.firstName<br/>
        @json_object.lastName<br/>        
        @json_object.address.city<br/>
        @json_object.address.postalCode<br/>
        foreach (dynamic phone in json_object.phoneNumber)
        {
            @phone.type<br/>
            @phone.number
        }

    }    
}

【问题讨论】:

    标签: json jquery-post asp.net-webpages


    【解决方案1】:

    如果数据只是像这样的键/值对,请不要JSON.stringify 数据。做一个表单发布:

    $('#json_form').submit(function (event) {
        event.preventDefault();
        var url = $(this).attr('action');
        var datos = {
            "uno": "lalala",
            "dos": "jojojo"
        }
        //var data = JSON.stringify(datos); no need for this
    
        $.post(url, datos, function (resultado) {
            $('#posted_values').html(resultado);
        });
    });
    

    然后可以从Request["uno"]Request["dos"] 获得值

    如果您确实需要使用JSON.stringify(对于更复杂的数据结构则需要),JSON 会在请求正文中传输,因此您需要从Request.InputStream 中提取它:

    var reader = new StreamReader(Request.InputStream);
    var json = reader.ReadToEnd();
    

    【讨论】:

    • 优秀的@Mike。我将使用我在 Wikipedia 中找到的 Json 对象将代码添加到显示此场景的问题中,我认为这对其他人有用。
    猜你喜欢
    • 2013-06-14
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 2019-06-18
    • 1970-01-01
    相关资源
    最近更新 更多