PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, September 6, 2022

[FIXED] When a button is clicked I want ajax to redirect towards a url but it is not working

 September 06, 2022     ajax, django, javascript     No comments   

Issue

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 class="banner">
    <div class="content">
        <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


Solution

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()),


Answered By - ShiBil PK
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing