veliyetisgengil
veliyetisgengil

Reputation: 101

How to post html code json array with ajax

If I dont any problem when myJson array have 1 object,but myJson array include 1 more than object I have problem that doesnot working post action.

        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);
            }
        });

error is 404 not found

Upvotes: 2

Views: 83

Answers (1)

Clement Okyere
Clement Okyere

Reputation: 321

try this

 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);
            }
        });

Upvotes: 1

Related Questions