JasonDavis
JasonDavis

Reputation: 48933

Send additional JSON data along with an existing AJAX post that is sending Serialized Form data

I want to send additional JSON data along with an existing AJAX form post.

I have this AJAX request that submits an HTML form with JavaScript...

// Serialize the data in the form
var serializedCommentData = $parentCommentForm.serialize();

// Make AJAX Save POST Request
commentAjaxRequest = $.ajax({
    url: ProjectManagementTaskModal.cache.urlEndPointsObj.createTaskCommentReplyUrl,
    type: 'post',
    data: serializedCommentData
});

I also jave a JSON string held in a JavaScript variable...

var jsonString = '[{"id":1,"name":"Kenneth Auchenberg","avatar":"http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif","type":"contact","value":"Kenneth Auchenberg"},{"id":9,"name":"Kenneth Hulthin","avatar":"http://cdn0.4dots.com/i/customavatars/avatar7112_1.gif","type":"contact","value":"Kenneth Hulthin"}]';

How can I make my JSON string in var jsonString be sent with the same AJAX request that the form sends?

Upvotes: 2

Views: 127

Answers (1)

m.antkowicz
m.antkowicz

Reputation: 13571

You can make an object with two fields

    var pack = { commentData : serializedCommentData, json: jsonString }

and serialize it then pass through the data.

    commentAjaxRequest = $.ajax({
        url: ProjectManagementTaskModal.cache.urlEndPointsObj.createTaskCommentReplyUrl,
        type: 'post',
        data: JSON.stringify(pack)
    });

You will need to unpack in the receiver script

Upvotes: 3

Related Questions