1

I have a JSON object which I encoded in Python. I don't know if I am doing the encoding right or not.

  proc = subprocess.Popen(['sshpass', 
                             '-p', 
                             password, 
                             'rsync', 
                             '-avz', 
                             '--info=progress2', 
                             source12, 
                             destination], 


                      stderr=subprocess.PIPE, 
                         stdout=subprocess.PIPE).communicate()[0]
progress = json.dumps(proc)
return HttpResponse(progress, mimetype="application/json")

I want to use the object 'progress' in the JAVASCRIPT to show the progress bar in the Django template. How it can be done? Thanks

I am trying to implement it in a Django template like this:

<script type="text/javascript" language="javascript">
function popUp() {
var jsProgress = JSON.parse(progress)
document.write(jsProgress)
}
</script>

But this shows nothing.

1
  • Can you explain a bit more clearly what you're doing? In particular, how are you sending the JSON to the browser? Commented Dec 11, 2012 at 9:43

1 Answer 1

1

You can decode any string(don't care about mime type) with JSON.parse("string") https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/JSON/parse

So just pass response from server to this method like that:

var jsObject = JSON.parse(server.response);
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. See my edited question. How it can be done in my case?
Tried: var message = JSON.parse(progress) document.write(message) but didn't work.
parese returns javascript object - it means that progress must be string which will be converted to message object. You can do: document.write(progress) if it's string, but not message

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.