sreisman
sreisman

Reputation: 668

How to use URL with JQuery .getJSON() Function

I am using the Django web framework and am attempting to update a status bar on the client side to show the progress of a file upload. In my Django view I created am returning the progress as a json object with the following:

response = HttpResponse(json.dumps(response_data), content_type="application/json")
        return response

On the webpage I attepmting to get this information with the following use of the $.getJSON() function

function status(importid){
    var barProgress = window.setInterval("getProgress(importid);", 1000);
}

function getProgress(importid){
    var url=api_server_address+"import/status/update/";
    console.log(url);
    $.getJSON(url,{impid: importid},
      /*UPDATE--if I check the url at this point it is what I expect, 
      the same url I hardcode below
      http://localhost:8000/status/update/?impid=258
      */
        function(data) {
            alert(data);
        }
    );
}

function loadBar(data){
    $('#progressBar').progressbar(load);
}

However when I check the response in the debugging console I keep getting an Internal 500 error. If instead of using the url variable and I pass the function a hardcoded url I do get my JSON results. For instance, the following returns the response I want:

$.getJSON("http://localhost:8000/status/update/?impid=258", function(data) {
    console.log(data);
});

I want to be able to dynamically change the url that is passed depending on the impid number which will be sent in the GET request. I have been trying to solve this problem for a long time now so I really appreciate any help that you may be able to offer. Thanks so much!

Upvotes: 0

Views: 4939

Answers (2)

dcodesmith
dcodesmith

Reputation: 9614

Try this with getJSON

function getProgress(importid){
    var url = api_server_address + "import/status/update/";
    console.log(url);
    $.getJSON(url, {impid: importid})
        .done(function(data) {
            console.log(data);
        });
}

Upvotes: 2

waldek_h
waldek_h

Reputation: 930

Try with full ajax approach, without shorthand getJSON:

var url=api_server_address+"import/status/update/";
var getProgress =  function(importId) {
     $.ajax({
        url: url,
        data: { impid: importId },
        type: "GET",
        dataType: "json"
     })
     .done(function(data){
         console.log(data);
     });
}

Upvotes: 1

Related Questions