Reputation: 3515
I'm new to all django, ajax things .. I read some tutorials on the net and I'm trying to make a simple form which posts some information via ajax.
Here is jquery part of my template :
<script type="text/javascript">
$(document).ready(function () {
$('#iEventAjax').submit( function()
{
var name = $('input[name=event_name]');
var data = name.val();
$.ajax({
type:"POST",
url:"/mediaplanner/edit/",
data:data,
success: function(msg){
alert(msg);
}
});
});
And the view code :
def iEventAjax(request):
if request.is_ajax():
return HttpResponse("ok")
else:
return render_to_response("iEventSave.html",{})
Well, when i post something, it returns iEventSave.html instead of giving the "ok" message. Any suggestions, which part do I fail ?
Upvotes: 0
Views: 1353
Reputation: 2219
What is happening with this code:
In jQuery you need to either return false from the submit function, or call preventDefault on the event jQuery Submit Docs
So:
$(document).ready(function () {
$('#iEventAjax').submit( function()
{
var name = $('input[name=event_name]');
var data = name.val();
$.ajax({
type:"POST",
url:"/mediaplanner/edit/",
data:{my_data:data},
success: function(msg){
alert(msg);
}
});
return false;
});
Upvotes: 5
Reputation: 29831
You want to do var data = name.serialize();
to create a query-string parameter.
Upvotes: 0