javascript - Django/AJAX/JQuery - Simple POST troubles -
javascript - Django/AJAX/JQuery - Simple POST troubles -
i'm trying implement simplest ajax post can think of because i'm new ajax , jquery. thought this: have button, , when clicked want submit basic post info same page (the page is: http://{{ ip address }}/django/ajax/ ).
in .html file have:
<script type="text/javascript"> function pythonizer(){ $("#msgid").append("it should post after appended"); $.ajax({ url: '/django/ajax/', type: 'post', data: {'obj': "test string"}, datatype: 'json', contenttype: "application/json; charset=utf-8", success: function(response) { alert(response); } }); } </script> <div id="msgid"> </div> <input type="button" id="mybutton" value="click me" onclick="pythonizer()" />
and relevant function in view.py is:
def ajax(request): if request.method == 'post': homecoming httpresponse("hello ajax") homecoming render_to_response('huh.html', {})
simple, right? problem however, when click button, "it should post after appended" added div, nil else happens, httpresponse("hello ajax") not returned, post isn't working!
what i've written seems consistent documentation i've read, have feeling i'm either missing stupid, or trying oversimplify post. in advance!
i don't know django or python, problem javascript it's not doing when ajax phone call completes.
<script type="text/javascript"> function pythonizer(){ $("#msgid").append("it should post after appended"); $.ajax({ url: '/django/ajax/', type: 'post', data: {'obj':data}, // <-- possible error? datatype: 'json', contenttype: "application/json; charset=utf-8", success: function (response) { alert(response); } }); } </script>
this alert whatever write response (presumably "hello ajax").
also, there's error in script: haven't defined 'data' (as in data: {'obj':data},
). script won't execute as-is, unless declared data
elsewhere.
javascript jquery html ajax django
Comments
Post a Comment