I'm implementing an HTTP server in C++, and sending Ajax POST requests to it via jQuery.
I have a form on one of my pages that I submit using this code:
$('#create-event form').submit(function(e) {
var postData = $(this).serializeArray();
var formURL = $(this).attr('action');
$.ajax({
url : formURL,
type: 'POST',
data : postData,
contentType: 'text/plain'
});
e.preventDefault();
});
When I receive this request in my C++, the last ~10 characters of the query parameters are a garbled mess, as if the encoding is wrong. The thing is, I have many of these type of forms, and this same garbling was happening to other forms, until I added the contentType: 'text/plain'
field. Now this form is the only one where this encoding problem is happening.
Is there any other type of encoding field that I can set to prevent this garbling? I've tried everything that I can find and nothing has helped.
Any help would be greatly appreciated, I've spent hours on this already.
EDIT: I now convert to unix timestamp client-side and there's no garbling, it must have had something to do with the URL-encoded space and colon.
to create a query string, you should try using $(this).serialize()
instead of serialize array.
$('#create-event form').submit(function(e) {
var postData = $(this).serialize();
var formURL = $(this).attr('action');
$.ajax({
url : formURL,
type: 'POST',
data : postData,
contentType: 'text/plain'
});
e.preventDefault();
});