【问题标题】:How to post html code json array with ajax如何使用ajax发布html代码json数组
【发布时间】:2019-09-07 09:57:59
【问题描述】:

如果当 myJson 数组有 1 个对象时我没有任何问题,但 myJson 数组包含的对象多于 1 个对象,我会遇到无法执行后操作的问题。

        var items = [];
        $('.grid-stack-item.ui-draggable').each(function () {
            var $this = $(this);

            items.push({
                ID: $this.attr('data-ItemId'),
                x: $this.attr('data-gs-x'),
                y: $this.attr('data-gs-y'),
                Width: $this.attr('data-gs-width'),
                Height: $this.attr('data-gs-height'),
                UserDashboardId: $this.attr('data-dashboardid'),
                content: $('.grid-stack-item-content', $this).html()
            });
        }); 


        myJson = JSON.stringify(items)
        console.log(myJson);
        $.ajax({
            type: 'POST',
            url: '/Dashboard/SendItem/?json=' + myJson,
            success: function (message) {

            },
            error: function (jqXHR, textStatus, errorThrown) {
                alert("jqXHR:" + jqXHR.status + " errorThrown: " + errorThrown);
            }
        });

错误是 404 未找到

【问题讨论】:

    标签: c# json ajax post


    【解决方案1】:

    试试这个

     var items = [];
            $('.grid-stack-item.ui-draggable').each(function () {
                var $this = $(this);
    
                items.push({
                    ID: $this.attr('data-ItemId'),
                    x: $this.attr('data-gs-x'),
                    y: $this.attr('data-gs-y'),
                    Width: $this.attr('data-gs-width'),
                    Height: $this.attr('data-gs-height'),
                    UserDashboardId: $this.attr('data-dashboardid'),
                    content: $('.grid-stack-item-content', $this).html()
                });
            }); 
    
    
            myJson = JSON.stringify(items)
            console.log(myJson);
            $.ajax({
                type: 'POST',
                url: '/Dashboard/SendItem/',
                dataType: "json",
                data: myJson 
                contentType: "application/json",
                success: function (message) {
    
                },
                error: function (jqXHR, textStatus, errorThrown) {
                    alert("jqXHR:" + jqXHR.status + " errorThrown: " + errorThrown);
                }
            });
    

    【讨论】:

    • 不客气。如果它适合您,您应该将其标记为正确答案
    猜你喜欢
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    相关资源
    最近更新 更多