Send 'POST' request without submit event
Event after which i am sending ajax is not submit. I even don't have sumbmin button. This is part of my jquery:
var description_data = $('#description_form').serialize();
alert(description_data)
$.ajax({
url: '/accounts/profile/add_description/',
type: 'POST',
data: description_data,
success: function(data){alert('ok')}
});
and my view.py
def add_description(request):
if request.is_ajax() or request.method == "POST":
form = DescriptionForm(开发者_StackOverflowrequest.POST, prefix="description")
if form.is_valid():
user = request.user
profile = user.get_profile()
description = form.cleaned_data['content']
profile.description=description
profile.save()
response = HttpResponse()
response['Content-Type'] = "text/javascript"
response.write(serializers.serialize('json', [profile], fields=('description')))
return response
else:
return HttpResponse('form invalid')
else:
return HttpResponse('error')
Ajas is sent not by submit event, but when it is send like that there request.is_ajax() or request.method=="POST"
return False. How can i make it working?
You probably need to call preventDefault, to stop the click event submitting the form before it runs the Ajax request.
$('#myButton').click(function(e) {
e.preventDefault;
//Your code from above
});
精彩评论