2013-06-27 71 views
1

我有一個問題,我的ajax調用,其中成功沒有按預期工作,我檢查控制檯和我的腳本在服務器端檢索和答案,我也做了一個console.debug調用爲了在控制檯上顯示答案而沒有發生。讀完恐懼後,我找不到錯誤。你能幫助我嗎?Jquery ajax succes不工作

我的AJAX

$.ajax({ 
      url: '{% url 'credentials.ajax.users_save' %}', 
      type: 'post', 
      dataType: 'json', 
      data: { 
       user_id: $('#user_id').val(), 
       username: $('#username').val(), 
       password: $('#password').val(), 
       last_name: $('#last_name').val(), 
       first_name: $('#first_name').val(), 
       email: $('#email').val(), 
       address: $('#address').val(), 
       phone_office: $('#phone_office').val(), 
       phone_home: $('#phone_home').val(), 
       expiration: $('#expiration').val(), 
      }, 
      success: function (msg) { 
       console.debug(msg) 
       if (msg == false || parseInt(msg) == 0) { 
        $.pnotify({ 
         title: '{% trans 'Error' %}', 
         text: '{% trans 'There were a error, please try again' %}', 
         type: 'error' 
        }); 

       } else { 
        $.pnotify({ 
         title: '{% trans 'Congrats' %}', 
         text: '{% trans 'User has been create' %}', 
         type: 'success' 
        }); 
        $('#modalCreateUser').modal('hide'); 
        window.setTimeout(function(){location.reload()}, 2000) 

       } 
      } 
     }); 

我的服務器端功能

def users_save (request): 
    try: 
     if request.is_ajax() and request.POST: 
      user_id = request.POST['user_id'] 
      s = get_current_site(request) 
      print s.id 
      u = User (
         username = request.POST['username'], 
         first_name = request.POST['first_name'], 
         last_name = request.POST['last_name'], 
         email = request.POST['email'] 
        ) 
      u.set_password(request.POST['password']) 
      up = UserProfile(
         address = request.POST['address'], 
         phone_home = request.POST['phone_home'], 
         phone_office = request.POST['phone_office'] 
          ) 
      up.user = u 
      up.site = s 
      #up.save() 
      u.save() 
      return HttpResponse(True, mimetype='application/json') 
    except: 
     return HttpResponse(False, mimetype='application/json') 
+0

在控制檯日誌中的任何語法錯誤? – karthikr

+0

您預期會發生什麼,您現在看到什麼錯誤或意外行爲? –

+0

NOne,沒有錯誤語法,服務器端腳本返回True作爲json模式的答案。那麼控制檯在我寫代碼時必須顯示一行「True」,但在控制檯上沒有答案,但reqwuest顯示True – Carlos

回答

0

我有我回答的

return HttpResponse(0, mimetype='application/json') 

return HttpResponse(False, mimetype='application/json') 

,而不是通知爲真爲Django的sintaxis的JavaScript和jQuery的不可能解析它,然後它不知道做什麼原因,我已經改變和這項工作做得很好,現在

非常感謝

1

看來你有一些語法錯誤.... 我已經標誌着他們在你的代碼下面評論

$.ajax({ 
     url: '{% url 'credentials.ajax.users_save' %}', 
     type: 'post', 
     dataType: 'json', 
     data: { 
      user_id: $('#user_id').val(), 
      username: $('#username').val(), 
      password: $('#password').val(), 
      last_name: $('#last_name').val(), 
      first_name: $('#first_name').val(), 
      email: $('#email').val(), 
      address: $('#address').val(), 
      phone_office: $('#phone_office').val(), 
      phone_home: $('#phone_home').val(), 
      expiration: $('#expiration').val() //, no comma here 
     }, 
     success: function (msg) { 
      console.debug(msg); // needed a semi-colon 
      if (msg == false || parseInt(msg) == 0) { 
       $.pnotify({ 
        title: '{% trans 'Error' %}', 
        text: '{% trans 'There were a error, please try again' %}', 
        type: 'error' 
       }); 

      } else { 
       $.pnotify({ 
        title: '{% trans 'Congrats' %}', 
        text: '{% trans 'User has been create' %}', 
        type: 'success' 
       }); 
       $('#modalCreateUser').modal('hide'); 
       window.setTimeout(function(){location.reload()}, 2000); // needed a semicolon 

      } 
     } 
    });