Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

141
Visualizações
django ajax post to unexpect url

my ajax as follow:

<script>
    $(document).ready(function () {
        $.ajaxSetup({
            data: {csrfmiddlewaretoken: '{{ csrf_token }}'}
        });
        $("#submit").submit(function () {
            var title = $("#title").val();
            var body = $("#body").val();
            var images = $("#browsefile")[0].files[0];

            $.ajax({
                type: "POST",
                data: {title: title, body: body, images: images},
                url: "{% url 'xxxxx' %}",
                success: function (result, statues, xml) {
                    alert(result);
                },
                error: function () {
                    alert("false");
                }
            })
        })
    })
</script>

and my urls pattern as follow:

from django.conf.urls import url
from django.contrib import admin
from django.conf import settings
from django.conf.urls.static import static

from blog import views

urlpatterns = [
    url(regex=r'^add/$', view=views.add, name='bbb'),
    url(regex=r'^newBlog/$', view=views.addblog, name='xxxxx'),
]

urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

I except ajax post data to the method addblog, but I get POST /add/ HTTP/1.1 instead. anyone can told me where i made a mistake, thanks!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Try adding event.preventDefault(); in the submit function to disable the default behavior of the form, which is to follow the action attribute.

$("#submit").submit(function (event) {
        event.preventDefault();
        var title = $("#title").val();
        var body = $("#body").val();
        var images = $("#browsefile")[0].files[0];
        formdata = new FormData();
        formdata.append("images", images);
        formdata.append("body", body);
        formdata.append("title", title);

        $.ajax({
            type: "POST",
            data: formdata,
            url: "{% url 'xxxxx' %}",
            success: function (result, statues, xml) {
                alert(result);
            },
            error: function () {
                alert("false");
            }
        })
        return false;
    })

EDIT: per comment.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda