Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to post form data in json format ? #531

Closed
krazylearner opened this issue Sep 8, 2017 · 3 comments
Closed

How to post form data in json format ? #531

krazylearner opened this issue Sep 8, 2017 · 3 comments
Labels

Comments

@krazylearner
Copy link

krazylearner commented Sep 8, 2017

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

@kevindb
Copy link
Contributor

kevindb commented Dec 4, 2017

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

@marosseleng
Copy link

marosseleng commented Jan 15, 2018

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!

@kevindb
Copy link
Contributor

kevindb commented Feb 13, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants