python - Ajax is giving back all the html instead of the variable in Django? -
i have ajax , template this:
<html> <body> <script language="javascript" type="text/javascript"> <!-- //browser support code function ajaxfunction(){ var ajaxrequest; // variable makes ajax possible! try{ // opera 8.0+, firefox, safari ajaxrequest = new xmlhttprequest(); } catch (e){ // internet explorer browsers try{ ajaxrequest = new activexobject("msxml2.xmlhttp"); } catch (e) { try{ ajaxrequest = new activexobject("microsoft.xmlhttp"); } catch (e){ // went wrong alert("your browser broke!"); return false; } } } // create function receive data sent server ajaxrequest.onreadystatechange = function(){ if(ajaxrequest.readystate == 4){ document.myform.time.value = ajaxrequest.responsetext; } } url = '/home' ajaxrequest.open("get", url, false); ajaxrequest.send(null); } //--> </script> <form name='myform'> {% csrf_token %} name: <input type='text' onchange="ajaxfunction();" name='username' /> <br /> time: <input type='text' name='time' id='time' value="" /> </form> </body> </html>
and have simple views this:
from django.shortcuts import render_to_response, httpresponse import simplejson django.template.context import requestcontext import datetime def home(request): if request.get: = datetime return httpresponse(simplejson.dumps(a), mimetype='application/json') #return render_to_response('home.html', {'a':a}, context_instance=requestcontext(request)) else: return render_to_response('home.html', context_instance=requestcontext(request))
ajax loading when press enter instead of particular variable template loading in input box. what's wrong?
this line:
if request.get:
checks see if there parameters. there aren't, because you're not sending any. url /home
. use if request.method == 'get'
, think you're checking wrong thing here: normal request (not ajax) also get.
what should send http_x_requested_with
header "xmlhttprequest", check request.is_ajax()
in view. or, recommended, use library jquery - sets automatically.
Comments
Post a Comment