I am working on building a Django project.
Once the button is clicked, the form will be submitted and some tasks will be performed based on the information from the form. However, in my case, the task can be done properly while there is always error pop up saying: "parsererror SyntaxError: Unexpected end of JSON input".
Here is my AJAX function:
$(document).on('submit', '#productForm', function(e){
e.preventDefault();
$.ajax({
method: 'POST',
dataType: 'json',
url: 'product/',
data: {
region: $("#Region-choice").val(),
country: $("#Country-choice").val(),
product: $("#Product-choice").val(),
dvn: $("#dvn").val(),
reship: $("#reshipCheckbox").val(),
reshipId: $("#reshipTextfield").val(),
validator: $("#Validator").val()}
})
.done(function(){
alert("Product Created!");
})
.fail(function(req, textStatus, errorThrown) {
alert("Something went wrong!:" + textStatus + ' ' + errorThrown );
});
alert("Submitted!");
});
Views function:
def viewCreateProduct(request):
"""The .delay() call here is to convert the function to be called asynchronously"""
if request.method == 'POST':
region = request.POST.get('region')
country = request.POST.get('country')
product = request.POST.get('product')
dvn = request.POST.get('dvn')
reship = request.POST.get('reship')
reshipId = request.POST.get('reshipId')
validator = request.POST.get('validator')
task = createProduct.delay(region, country, product, dvn, reship, reshipId, validator)
return HttpResponse('')
.fail()condition is shown? In which case you need to look at what your Django view is returning, since obviously that's not valid JSON. Please look in your browser developer tools what is being return, and if that's not what you expect, please show us the Django view for/product/urlapplication/json(nottext/html). You can use theJsonResponseclass instead ofHttpResponseor set the content-type header of yourHttpResponse. But main thing is: set the content to some valid JSON.