I am quite new to django and jquery. I am trying to perform a post request on:
my model
class part_list(models.Model):
designID = models.ForeignKey('modules', on_delete=models.CASCADE)
PartID = models.ForeignKey('parts',on_delete=models.CASCADE)
quantity = models.IntegerField()
def __str__(self):
return "DesignID :{}, PartID :{}".format(self.designID,self.PartID)
class Meta:
unique_together = (('designID','PartID'))
my serializer
class sub_part_list_serializer(serializers.ModelSerializer):
class Meta:
model = part_list
fields = ('designID', 'PartID', 'quantity')
my view
class sub_part(generics.ListCreateAPIView):
serializer_class = sub_part_list_serializer
def get_queryset(self):
queryset = part_list.objects.all()
designID = self.request.query_params.get('designID',None)
print(self.request)
if designID is not None:
queryset = queryset.filter(designID__exact=designID)
return queryset
and my jquery
$('#test').on('click', function(){
var data1 = {};
data1["designID"] = "bar-123";
data1["PartID"] = "A102";
data1["quantity"] = 3;
console.log(data1);
$.ajax({
type: "POST",
url: "bon/sub_part/",
data: data1,
contentType: "application/json; charset=utf-8",
success: function(data){
alert(data);
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
alert("some error " + String(errorThrown) + String(textStatus) + String(XMLHttpRequest.responseText));
}
});
the error it is showing is
Bad Requesterror{"detail":"JSON parse error - Expecting value: line 1 column 1 (char 0)"}
when I am making post requests directly from the API it is working fine. I am not using any user authentication. I also tried to add the csrf token in the request header but nothing changed
Please Help!