2016-09-26 58 views
0

我正在研究铬扩展。目前,它只是检查用户是否存在。AJAX保持检测失败,但没有显示错误

在插件弹出窗口中有一个窗体。用户使用他的用户名和密码填写此表单,并将其发送到服务器。

用户在查看发现,一切看起来正常运行,但AJAX出于某种原因返回失败。

这是一个观点:

@csrf_exempt 
def api_authentication(request): 
    if request.method == 'POST': 
     login = request.POST.get('login') 
     password = request.POST.get('password') 
     if login and password: 
      username = login 
      if '@' in login: 
       username = mainapp_models.User.objects.get(email=login).username 
       if not username: 
        return JsonResponse({'success':0}) 
      user = authenticate(username=username,password=password) 
      print user # IT's ok, user is found 
      if user: 
       return JsonResponse({'username':user.username, 
            'success':1}) 
    return JsonResponse({'success':0}) 

这是AJAX:

$(document).ready(function() { 

    $('#login-button-id').click(function() { 

     var login = $('#input-email-id').val(); 
     var password = $('#input-password-id').val(); 
     $.ajax({ 
      url: 'http://127.0.0.1:8000/api/authenticate', 
      type: 'POST', 
      data: { 
       'login': login, 
       'password': password 
      }, 
      success: function() { 
       alert('success'); 
      }, 
      error: function() { 
       alert('failure'); 
      } 
     }) 
    }) 
}); 

而且JsonResponse看起来是这样的:{'username': u'milano', 'success': 1}

当我填写表格并发送数据,user是发现但'失败'被警告。你知道有什么问题吗?

编辑:

警报安迪的回答是:

由于console.log没有工作,我改成了警报:

error: function (jqXHR, textStatus, errorThrown) { 
       alert("JQuery failed: " + textStatus + " with error thrown: " + errorThrown); 
       alert(jqXHR.responseText); 
      } 

警报:

enter image description here enter image description here

EDIT2

终于找到了控制台日志:多个

enter image description here

+0

首先有一个错字你的发布代码,'uccess'而不是'success'。然后,当错误callabck被解雇时,你会得到哪个错误?! –

+0

我尝试使用下面答案中的代码检查错误。它不会记录任何内容,因此我将日志更改为警报。结果在问题的底部。 –

+0

我找到了控制台日志。我不得不点击插件 - 检查插件,并删除所有警报,因为他们删除检查窗口。我在问题的底部添加了控制台日志。 –

回答

0

有点调试是得心应手。试着改变你的AJAX误差函数来报告多一点(不多,但它可以帮助):

error: function(jqXHR, textStatus, errorThrown){ 
    console.log("JQuery failed: " + textStatus + " with error thrown: " + errorThrown); 
    console.log(jqXHR); 
    alert('AJAX failed. Check browser console for more info'); 
} 

而且,你从开发服务器(./manage.py的runserver)运行你的Django应用程序?做到这一点,并寻找任何有用的输出,如堆栈跟踪。可能是作为一个错字或Python代码

+0

哦,并发布浏览器控制台日志在这里,所以我们可以看到它:) –

+0

这就是问题,它不写入任何东西到控制台。我正在实时检查...不知道为什么.. –

+0

嗯,没有大量的帮助。您的浏览器中的开发者网络标签是什么作为响应提供的? –

-1

另一项建议被遗忘的进口一样简单 - 看你的Python接近尝试改变:

if user: 

if user is not None: