At this all evening and can't see what I'm missing:
JS:
$(document).ready(function() {
$("#form").submit(function () {
var txt = $('textarea[name=text]').val();
$.ajax({
type: "POST",
url: "/parse_text/",
data: {"text": txt},
dataType: "text",
success: function(h) {
alert('ok');
},
error: function() {
alert('failed');
}
});
});
});
And the django code:
def parse_text(request):
return HttpResponse("hello", mimetype="text'/plain")
I can't get it to trigger the success method. Using firebug I can see that django is return "hello" and a status of 200.
I also tried changing to json (though what I really want is to return an html table).
In js:
dataType: "json",
In view:
return HttpResponse('[{"hello": "hello"}]', mimetype="application'/json")
Now return [{"hello": "hello"}] and 200 status.
What on earth am I doing wrong!?
Final working code:
$(document).ready(function() {
$("#form").submit(function () {
var txt = $('textarea[name=text]').val();
$.ajax({
type: "POST",
url: "/parse_text/",
data: {text : txt},
success: function(html) {
$("#actions").html(html);
},
error: function() {
alert("fail");
}
});
return false;
});
});
and in the view:
def parse_text(request):
html = ''
text = request.POST['text']
... generated some html based on the text ...
return HttpResponse(html)