Home > Enterprise >  When a button is clicked I want ajax to redirect towards a url but it is not working
When a button is clicked I want ajax to redirect towards a url but it is not working

Time:08-26

I am trying to create a SPA, using django and javascript, I have a home page with two buttons on it (Sign up and sign in), I want that when signup button is clicked, ajax redirects towards signup url Here is my code

js


$('#id_register').click(function(){
    $.ajax({
        url: 'register',
        method:"GET",
        success: function(data){
            console.log(data)
        },
        error: function(error){
            console.log(error)
        }
    })
})

Here is my html

<div >
    <div >
        <h3 id="home">Feeling Hungry?</h3>
        <h4 id="home_para">Order right now!</h4>
        <div>
            <button id="id_sign" type="button">Sign In</button>
            <button id="id_register" type="button">Sign Up</button>
        </div>
    </div>
</div>
</body>

It is successfully returning me the data but not redirecting towards the required url.

here is my urls.py

urlpatterns =[
    path('register', RegisterView.as_view()),
    path('login', LoginView.as_view(), name="login"),
    path('', HomeView.as_view(), name="home")
]

I will be very thankful for the help

CodePudding user response:

try using this

$('#id_register').click(function(){
    $.ajax({
        url: '/register/',
        method:"GET",
        success: function(data){
            console.log(data)
        },
        error: function(error){
            console.log(error)
        }
    })
})

then the urls.py

path('register/', RegisterView.as_view()),

CodePudding user response:

You don't need AJAX for this. If you want to redirect the user to another page, don't use a button with AJAX attached to it, use an tag instead.

  • Related