jquery-form/form

How to post form data in json format ?

krazylearner opened this issue · 3 comments

Description:

I need to send form data in json format . I tried sending it in 'data' attribute but it seems not working .

can you please give working example of sending form data in json format tthrough this module ?

i tried something like this

function showResponse(data, textStatus) {
 console.log(data);
}

var requestObject = {};
function showRequest(arr, $form, options) {
 for (var i = 0; i < arr.length; ++i) {
  requestObject[arr[i].name] = arr[i].value;
 }
}

var options = {
 target: '#output2', // target element(s) to be updated with server response 
 beforeSubmit: showRequest, // pre-submit callback 
 success: showResponse, // post-submit callback 
 contentType: "application/json",

 // other available options: 
 url: 'http://localhost:3000/v1/checkout/createContact', // override for form's 'action' attribute 
 type: 'post', // 'get' or 'post', override for form's 'method' attribute 
 dataType: 'json', // 'xml', 'script', or 'json' (expected server response type) 
 data: requestObject
};

// bind to the form's submit event 
$('#contact-form').submit(function() {
 $(this).ajaxSubmit(options);
 return false;
});

thanks

It looks like you're working a little too hard. Try just using the ajaxForm method, like the example in the Quick Start Guide.

$('#contact-form').ajaxForm();

Hi!
I got pretty much the same problem. I need my forms to be submitted as JSON with AJAX, so I wrote myself some very basic function that handles correctly just text inputs...

I bumped into this library when I searched for "form" and "JSON" but me neither can find any info whether JSON serialization is possible with this or not.

If it is not, my question is then, whether there is a plan to introduce support for JSON serialization.

If not, can anyone please explain to me, why this request is so uncommon? (I am more a backend-guy.)

Thank you for any clearance, or advice!

Please review the documentation at https://jquery-form.github.io/form/
The main methods, ajaxForm and ajaxSubmit, will serialize the form data to JSON and submit it via jQuery AJAX call.