You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django中Ajax提交注册表单出现Errno 10053错误求助

Hey there! Let's break down this Errno 10053 issue you're facing—even though user registration works, that error is definitely worth fixing to keep your app stable.

What's Causing Errno 10053?

On Windows, this error translates to "An established connection was aborted by the software in your host machine". Since your user registration succeeds, the backend logic (saving the user) has already completed. The problem happens when the server tries to send the final JSON response back to your browser, but the browser has already closed the connection.

Here are the most likely reasons:

  • Synchronous email sending is blocking the response: Your view sends a confirmation email directly during the request. If this takes too long (e.g., SMTP server delays), the browser might timeout and abort the connection before the server finishes sending the response.
  • Default Django dev server limitations: The built-in wsgiref server is a simple, single-threaded tool for development only. It has known stability issues with long-running requests and can drop connections unexpectedly.
  • Unintended client-side termination: While less likely here, if any code closes your registration modal or navigates away before the Ajax response is fully received, the browser could abort the connection.

Fixes to Resolve the Error

Let's tackle these issues one by one:

1. Send Confirmation Emails Asynchronously

The biggest culprit is almost certainly the synchronous email task. Move this work to a background worker so your view can return the response immediately. Here's how to do it with Celery (Django's most popular task queue):

First, set up Celery with your Django project (follow official docs for your Celery version). Then create a task for emails:

# tasks.py in your app
from celery import shared_task
from django.core.mail import send_mail
from django.conf import settings

@shared_task
def send_confirmation_email(to_email, username):
    # Paste your existing email-sending code here
    subject = "Confirm your registration"
    message = f"Hi {username},\nPlease click the link below to activate your account..."
    send_mail(
        subject,
        message,
        settings.DEFAULT_FROM_EMAIL,
        [to_email],
        fail_silently=False,
    )

Update your view to trigger the task asynchronously:

# views.py
from .tasks import send_confirmation_email

class Register(View):
    form_class = signupForm
    def post(self, request):
        data = {'errMsg': ''}
        form = self.form_class(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            to_email = form.cleaned_data['email']
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user.set_password(password)
            user.save()
            # Trigger async email task instead of sending synchronously
            send_confirmation_email.delay(to_email, username)
        else:
            data = { 'errMsg': 'Something went wrong.' }
        return JsonResponse(data)

2. Use a More Reliable Development Server

Replace the flaky wsgiref server with a tool built for better connection handling:

  • Install django-runserver-plus (uses Werkzeug under the hood):
pip install django-runserver-plus
  • Add it to your INSTALLED_APPS in settings.py:
INSTALLED_APPS = [
    # ... your other apps
    'django_extensions',
]
  • Run the server with:
python manage.py runserver_plus

This server handles connections far more reliably than the default one.

3. Adjust Ajax Timeout (If Needed)

Give your server extra time to respond before the browser aborts the connection by adding a timeout parameter to your Ajax call:

$("#registerForm").on('submit', function(e) {
  e.preventDefault();
  $.ajax({
    type: 'POST',
    url: "/authenticate/register/",
    data: {
      'email' : $('#inputEmail').val(),
      'username': $('#inputUsername').val(),
      'password': $('#inputPassword').val(),
      csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val()
    },
    dataType: 'json',
    timeout: 15000, // 15 seconds (adjust as needed)
    success: function(data){
      if (data['errMsg'] != '') alert(data['errMsg']);
      else alert('Sending confirmation link...');
    },
    error: function(xhr){
      alert('error!');
    }
  });
});

4. Check for Client-Side Connection Closure

Double-check that your registration modal isn't being closed programmatically before the Ajax response is received. In your current code, the alert in the success callback blocks execution, so this isn't an issue—but if you later add code to close the modal, make sure it runs only after the response is processed.

Final Notes

The async email task is the most critical fix here—it eliminates the long-running synchronous operation that's causing the browser to timeout. Switching to a better dev server will also help prevent connection-related bugs from the default wsgiref tool.

内容的提问来源于stack exchange,提问作者Ahtisham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 08:17:36