0

I am trying to post my formdata via Ajax. But for some reason my payload is shown as a string and not as ajax:

shopName=asfd&street=afs&houseNo=asf&zipcode=&city=&country=&phoneNumber=&fax=&email=&website=

But it shouls appear as JSON. I have got no idea what could be wrong.

The code I have to compose my request is:

$('#shopForm').submit(function(e){
e.preventDefault();
var formData = $('#shopForm').serialize();
submitForm(formData);
});

function submitForm(formData)
{
$.ajax({
    url: '/admin/shop/ajax',
    type: 'POST',
    dataType: 'json',
    contentType: 'application/json; charset=utf-8',
    data: formData,
    success: function(response) {
        console.log('succes');
        console.log(response);
    },
    error: function(response) {
        console.log('error');
        console.log(response);
    }

});
}

Any help is well appreciated.

Thanks.

EDIT 1

When changing data: formData, tot data: JSON.Stringify(formData),

The payload changes to

shopName=asfd&street=afs&houseNo=asf&zipcode=&city=&country=&phoneNumber=&fax=&email=&website= No Properties

sanders
  • 10,794
  • 27
  • 85
  • 127
  • Possible duplicate: http://stackoverflow.com/questions/6323338/jquery-ajax-posting-json-to-webservice – thexacre Apr 27 '14 at 08:31
  • @mit I have tried to use JSON.Stringify but that does not solve the problem. – sanders Apr 27 '14 at 08:34
  • What are you expecting here? You have a form, and you serialise it. According to the manual of that function, now you have one big string. The best you'll get from that when making it in to JSON is not going to be a JSON object as you seem tot expect I presume. Check out how to actually do that by, for instance, looking at question s like these: http://stackoverflow.com/questions/1184624/convert-form-data-to-js-object-with-jquery – Nanne Apr 27 '14 at 08:42
  • @Nanne that generates a JSON Object why can't i get it in a simple array with key value pairs? – sanders Apr 27 '14 at 09:07
  • You can, ik just not sure why you seem to want to serialize it first. That makes it into a single string, and then there is no reason I can think of how that would help :) – Nanne Apr 27 '14 at 10:14
  • So @Nanne how can I get it into an array? – sanders Apr 27 '14 at 10:20

0 Answers0