ParadoxicalEnigma
ParadoxicalEnigma

Reputation: 159

Simple ajax GET request not working on my Django webpage

I'm very new to both Django and Ajax (it's my first time using both) and I am receiving an error with my code. Basically I have an HTML button, on a webpage generated by a template in Django, that should call a function onclick which is assigned using jquery. When clicked, the button should send a GET request to the webpage with the specified url. That's all I want it to do, however, it just does not seem to work. It keeps throwing an error. Here is some of the code:

<button class="btn btn-primary toChange">Click me</button>

and

    $(".toChange").click(function(){
        $.ajax({
            url: 'localhost:8000/MyApp/ajax_test',
            type: 'GET', 
            success: function(data) {
                alert(data);
            },
            error: function(jqXHR, textStatus, errorThrown) {
                console.log(JSON.stringify(jqXHR));
                console.log("AJAX error: " + textStatus + ' : ' + errorThrown);
            }
        }); 
      //alert("Something happened!")
    });


</script>

and my view for what I'm trying to get is:

def test(request):
    return HttpResponse("Congrats, you've got it!")

Currently, I am using firefox to look at the webpage on localhost:8000. If I look at the console when I click the button, this is what I see.

{"readyState":0,"status":0,"statusText":"[Exception... \"<no message>\"   nsresult: \"0x805e0006 (<unknown>)\"  location: \"JS frame :: https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js :: .send :: line 5\"  data: no]"}

AJAX error: error : [Exception... "<no message>"  nsresult: "0x805e0006 (<unknown>)"  location: "JS frame :: https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js :: .send :: line 5"  data: no]

I tried researching the errors and I'm not sure but it could be an error with firefox not allowing the request to go through. Can someone help me please?

Upvotes: 1

Views: 930

Answers (1)

ruddra
ruddra

Reputation: 51978

Change your url from:

$(".toChange").click(function(){
    $.ajax({
        url: 'localhost:8000/MyApp/ajax_test', # change here

to

$(".toChange").click(function(){
    $.ajax({
        url: '/MyApp/ajax_test',

Upvotes: 3

Related Questions