Possible Duplicate:
django file upload from json

Hi am using the following ajax upload from the template but i do not get a response from django view.What is wrong here..i do not see any alert

function ajax_upload(formid)
{
var form = $(formid);
form.ajaxSubmit({
  dataType:  'json',
  success:   function (data) {
  alert("Hereeeeeeee");
  if(data.status == '1')  
  {
     alert("Uploaded Successfull");
  }
  else 
  {
     alert("Uploaded UnSuccessfull :(");
  }
  }
} )   ; 
}

EDIT Django:

  def someview(request):
      response_dict={'status':1}
      logging.debug("seen") //This is seen in the logs
      return HttpResponse(simplejson.dumps(response_dict), mimetype='application/javascript')

EDIT1

Please also for complete source code look at django file upload from json

有帮助吗?

解决方案

From your code:

form.ajaxSubmit({
  dataType:  'json',
  success:   function (data) {
    alert("Hereeeeeeee");

Your callback executes on 'success', so we can narrow down the failure to the django view side. Assuming everything is syntactically correct, my best guess is that since you're returning json data, your response mimetype should be:

mimetype='application/json'.

If that doesn't work, I would suggest using Firebug on Firefox or Developer Tools on Chrome to look at the server response. You should be able to see a django stacktrace there.

其他提示

Check that someview added to urls.py.
Check that {% csrf_token %} added to form

It's rather impossible you will see that log, hence this is a python syntax error:

response_dict{'status':1}

and should be:

response_dict = dict(status=1)

or:

response_dict = {'status':1}

or:

response_dict = dict()
response_dict['status'] = 1

or:

response_dict = dict()
response_dict.update({'status':1})
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top