1

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('')
4
  • Just to be clear: the alert from the .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/ url Commented Nov 20, 2018 at 15:02
  • @dirkgroten Yes, the alert from .fail() is shown. And nothing is returned since I just need to triger the task in views.py. I have attached the views as well. Commented Nov 20, 2018 at 15:08
  • You need to return valid JSON, an empty string isn't valid JSON. Also to be sure, the content type you return should be application/json (not text/html). You can use the JsonResponse class instead of HttpResponse or set the content-type header of your HttpResponse. But main thing is: set the content to some valid JSON. Commented Nov 20, 2018 at 15:09
  • @dirkgroten yep, it is solved. Please add your comment in the answer and I will mark it as the solution. Thx. Commented Nov 20, 2018 at 15:19

1 Answer 1

1

As the error says, you need to return valid JSON from your Django view:

  • An empty string isn't valid JSON, so make sure your response has valid JSON as its body
  • Also (although probably not critical in this case) you should set the Content-Type header to application/json instead of text/html which is the default for an HttpResponse. You can use Django's JsonResponse instead or add the correct HTTP header to your HttpResponse object.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.