Note that there are some explanatory texts on larger screens.

plurals
  1. POData not sent using jquery .ajax() to a Django view
    text
    copied!<p>I am trying to create a single ajax call. The ajax call is sent to server but the data then I am sending is not available in the request object in the view. When I print <code>request.post</code>, it gives <code>&lt;QueryDict: {}&gt;</code>. No data is being sent to the server. I know that the browser is sending the data since I can view it in the request payload in chrome. </p> <p><strong>script:</strong></p> <pre><code>$("#chatform").submit(function(e) { e.preventDefault(); //serialText = $(this).serialize(); var userText = $("#usertext").val(); var xmlRequest = $.ajax({ type: "POST", url: "/sendmessage/", data: {'tosend': userText}, //dataType: 'json', contentType: "application/json; charset=utf-8", success: function(data){ appendMessageSent(data.messagesent); } }); }); </code></pre> <p><strong>view.py:</strong></p> <pre><code>def send_message(request): if request.is_ajax(): message = "The hell with the world" print request.POST json = simplejson.dumps( {'messagesent' : request.POST['tosend']+"This is how we do it"} ) return HttpResponse(json, mimetype='application/javascript') </code></pre> <p><strong>html</strong></p> <pre><code>&lt;form id="chatform" action="" method="POST" &gt; &lt;input type='hidden' name='csrfmiddlewaretoken' value='8idtqZb4Ovy6eshUtrAiYwtUBboW0PpZ' /&gt; &lt;input type="text" name="chatarea" id="usertext"/&gt; &lt;input type="submit" value="Send"&gt; &lt;/form&gt; </code></pre> <p>I get an error saying that key <code>tosend</code> is not found in <code>request.post</code> dict.</p> <p><code>MultiValueDictKeyError: "Key 'tosend' not found in &lt;QueryDict: {}&gt;</code></p> <p>Can any one tell me why the data is not being sent to the server and/or why I can't access it in my view?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload