jqueryjsoncontent-typejquery-ajaxq

Input JSON is invalid. Request body must be valid JSON if content type is application/json


I am passing the following JSON object to my AJAX call

var contact = {
   "accessToken": "xxx-xxx-xxx-xxx-xxx",
   "email": "test@test.com",
   "customFields": {
      "custom7": {"value": "test rep"},
      "custom8": {"value": "test title"},
      "custom9": {"value": "test@localdev.com"}
   }
}

And this is my AJAX call:

$.ajax({
   type: 'POST',
   url: 'https://api.mydomain.com/v1/contacts/save',
   data: contact,
   dataType: 'json',
   success: function(data) {
     alert(data.errors);
   }
});

data.errors shows the following:

Input JSON is invalid. Request body must be valid JSON if content type is application/json

I checked above JSON object with jsonlint.com and it was validated. I don't understand where I am doing wrong!


Solution

  • stringify that json before passing it through -

    $.ajax({
        type: 'POST',
        url: 'https://api.mydomain.com/v1/contacts/save',
        data: JSON.stringify(contact),
        dataType: 'json',
        success: function(data) {
            alert(data.errors);
        }
    });