2

I'm using PyCharm (IDE for DJango Framework), it seem to work but I have this error

error: [Errno 10053] An established connection was aborted by the software in your host machine

When I look at the console when I clicked the button for Dajaxice call back, it says

Dajaxice: Something went wrong. 

Here's the template:

 <input type="submit" class="btn btn-primary" value="Display" id="showbutton" onclick="Dajaxice.tbl.sayhello(my_js_callback);">

My App name is tbl for some reason.

ajax.py

from django.utils import simplejson
from dajaxice.decorators import dajaxice_register

@dajaxice_register
def sayhello(request):
  return simplejson.dumps({'message':'Hello World'})

url.py

from django.conf.urls import patterns, include, url
from dajaxice.core import dajaxice_autodiscover, dajaxice_config
dajaxice_autodiscover()
from django.contrib import admin
admin.autodiscover()

urlpatterns = patterns('',
                   url(r'^$', 'tbl.views.display_maps', name='home'),
                   url(r'^admin/', include(admin.site.urls)),
                   url(r'^accounts/auth/$', 'tbl.views.auth_view'),
                   url(r'^accounts/logout/$', 'tbl.views.logout'),
                   url(r'^profile/$', 'tbl.views.user_profile'),
                   url(r'^accounts/loggedin/$', 'tbl.views.loggedin'),
                   url(r'^accounts/invalid/$', 'tbl.views.invalid_login'),
                   url(r'^landinfo$', 'tbl.views.landpin_info'),
                   url(r'^simple-autocomplete/', include('simple_autocomplete.urls')),
                   url(dajaxice_config.dajaxice_url, include('dajaxice.urls')),
                )

I don't know what's wrong. I followed the documentation for Dajaxice but again I got this error when running the app.

self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 10053] An established connection was aborted by the software in your host machine
Sachi Tekina
  • 1,662
  • 5
  • 25
  • 47

2 Answers2

0

The dajaxice 'Something went wrong' error message can result from various problems including missing or misspelled parameter names in your python function or exceptions generated in your JavaScript function. However, your example is very simple. There are no named parameters in the python function and you didn't provide the code for the JavaScript function. Your example follows the pattern of a similar problem I debugged and corrected in my own code.

It is related to the use of the onclick handler for a submit button. I believe that making an ajax call from a submit button handler initiates asynchronous activity on the server at the same time that the server is processing the POST request generated by submitting a form. I believe that the the server sees the POST request before it finishes responding to the ajax call and the POST preempts the ajax call, resulting in the dajaxice error message.

Debugging and getting to the root cause is rather involved but I played with test cases extensively and can consistently reproduce the behavior with a Safari browser and the Django python development server.

Both the browser and the server must handle asynchronous messages in order for ajax calls to work. I could be wrong about the server allowing this preemption. Rather, it could be your browser. Regardless, the end result is the same.

So how do you avoid this problem? In my case I added a hidden input element to the form and attached the data I would have sent with ajax to that input's value field. The hidden data is then automatically sent along with other form data in the POST that gets automatically generated when you submit the form.

There are other solutions: submit all form data via ajax and disable the automatic submit POST of form data (Submitting HTML form using Jquery AJAX). Or you could use some event not connected with form submission to send data to the server with ajax.

Community
  • 1
  • 1
nmgeek
  • 1,870
  • 1
  • 18
  • 29
0

What version of Django are you using?

Dajaxice is a dead project and doesn't work with Django >1.5. (See my other post.)

Community
  • 1
  • 1
Travis
  • 1,889
  • 1
  • 21
  • 34