2011-12-29 27 views
0

我正在對我的Ajax帖子函數使用以下視圖,並且調用重複失敗。我似乎在這裏得到了什麼問題。任何人都可以指出來Ajax帖子在Django視圖中給出錯誤

def interview(request): 
    if request.is_ajax(): 
     if request.method == 'POST': 
      name = request.POST.get('name', False) 
      email = request.POST.get('email', False) 
      skype_id = request.POST.get('skype_id', False) 
      phone = request.POST.get('phone', False) 
      time_slot_1 = request.POST.get('time_slot_1', False) 
      time_slot_2 = request.POST.get('time_slot_2', False) 
      time_slot_3 = request.POST.get('time_slot_3', False)   

      recipients = ['[email protected]'] 

      subject = 'Interview Appointment' 
      message = "The following person has registered for interview \n Name: %s \n Email: %s \n Skype Id: %s \n Phone: %s \n Slot-1: %s \n Slot-2: %s \n Slot-3: %s" % (name, email, skype_id, phone, time_slot_1, time_slot_2, time_slot_3) 

      from django.core.mail import send_mail 
      send_mail(subject, message, sender, recipients) 
      return_message = "Sent mail" 
      return HttpResponse(return_message,mimetype='application/javascript') 

我的JavaScript也非常簡單,但我不知道爲什麼我會收到錯誤消息

var email_val = $("#emailp").val(); 
var skype = $("#skypeId").val(); 
var phone = $("#phone_no1").val(); 
var t1 = $("#time_slot1").val(); 
var t2 = $("#time_slot2").val(); 
var t3 = $("#time_slot3").val(); 

$.ajax({ 
    type: "POST", 
    url: "/interviews/interview_form/", 
    data: { 
     'name': name_val, 
     'email': email_val, 
     'skype_id': skype, 
     'phone': phone, 
     'time_slot_1': t1, 
     'time_slot_2': t2, 
     'time_slot_3': t3, 
    }, 
    success: function(){ 
     alert("call successful"); 
     $('#complete').html("Form has been Submitted"); 
     }, 
    error: function(){ 
     alert("call failed"); 
     $('#error').html("Form could not be Submitted! Please try again"); 
     }, 

}); 

任何見解,歡迎選購。

回答

0

您還需要提交CSRF令牌以防止XSS攻擊。您的代碼中最直接的方法是將csrfmiddlewaretoken: '{{ csrf_token }}'添加到您的data字典中。另外,請確保您的視圖返回context_instance,以便CSRF令牌可用。

+0

那麼我在這裏使用的解決方案,所以我不認爲'csrf令牌'會導致問題。因爲我有另一個非常相似的看法,它工作得很好 – Sachin 2011-12-29 09:29:29