Reputation: 6555
Using Django I'm getting the following error when making a POST to my API:
The format indicated 'text/plain' had no available deserialization method. Please check your formats
and content_types
on your Serializer."
I have tried adding the enctype="application/x-www-form-urlencoded
to the form but the error is the same. I'm thinking maybe this is a API serializer issues?
Any idea's?
This is the AJAX:
$.ajax({
url: '/api/v1/rewards/campaigns/',
type: 'POST',
dataType: "json",
beforeSend: function (request) {
request.setRequestHeader("X-CSRFToken", $('input[name="csrfmiddlewaretoken"]').val());
},
data: $('#registration').serialize(),
success: function(data, textStatus) {
console.log('success');
},
error: function(errorThrown){
// data = JSON.parse(errorThrown.responseText);
console.log(errorThrown);
}
});
This is the resource it is posting to:
class urlencodeSerializer(Serializer):
formats = ['json', 'jsonp', 'xml', 'yaml', 'html', 'plist', 'urlencode']
content_types = {
'json': 'application/json',
'jsonp': 'text/javascript',
'xml': 'application/xml',
'yaml': 'text/yaml',
'html': 'text/html',
'plist': 'application/x-plist',
'urlencode': 'application/x-www-form-urlencoded',
}
def from_urlencode(self, data, options=None):
""" handles basic formencoded url posts """
qs = dict((k, v if len(v) > 1 else v[0] )
for k, v in urlparse.parse_qs(data).iteritems())
return qs
def to_urlencode(self, content):
pass
class CampaignCreateResource(ModelResource):
class Meta:
queryset = Campaign.objects.all()
resource_name = 'rewards/campaigns'
allowed_methods = ['post', 'get']
serializer = urlencodeSerializer()
validation = FormValidation(form_class=CampaignForm)
Upvotes: 1
Views: 1553
Reputation: 15864
Add contentType: 'application/json; charset=UTF-8'
to your $.ajax()
call to indicate the content type of the request data.
dataType
argument specifies the format of the response, not the request!
Upvotes: 2