我发送ajax之后的事件不提交。我甚至没有sumbmin按钮。这是我的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')}
});
和我的view.py
def add_description(request):
if request.is_ajax() or request.method == "POST":
form = DescriptionForm(request.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不是通过提交事件发送的,但是当它发送时,request.is_ajax() or request.method=="POST"
返回False。我怎样才能让它发挥作用?
答案 0 :(得分:2)
您可能需要调用preventDefault,以便在运行Ajax请求之前停止提交表单的click事件。
$('#myButton').click(function(e) {
e.preventDefault;
//Your code from above
});