2016-04-22 54 views
1

所以我有一个服务器应该返回一个JSON令牌进行身份验证。但它没有返回任何东西(我将它打印到控制台),甚至没有错误信息。主要是,它已经发回了一次或两次的错误消息。我试过的是充实完成和错误功能。我知道我的表单元素正在被拾取,因为他们打印到控制台。我有人确认服务器使用Ajax请求,但这不会。由于显而易见的原因,我没有发布服务器URL。为什么服务器不给我任何回应?

$(function() 
    { 

    $("#login").click(function() 
    { 
    var username = $("#username").val(); 
    var password = $("#password").val(); 
     login(username, password); 

    }); 

     function login(username, password) 
     { 
     $.ajax 
     ({ 
     url: "/", 
     type: "POST", 
     dataType: 'json', 
     contentType: 'application/json', 
     data: '{"username": "' + username + '", "password" : "' + password + '"}', 
     success: function(data, text, xhr) 
     { 
      console.log(xhr.status); 
      console.log(data); 
      console.log(text); 

     }, 
     error: function(xhr, status, error) 
     { 
      console.log("Error" + xhr + status + error); 
     }, 
      complete: function(xhr, text) 
      { 
      console.log(xhr.status, text); 
      } 
     }); 

     } 

    }); 

在铬,它没有给我任何错误。但在Firefox:

POST file/ HTTP/1.1 200 OK 
Error has occurederror jquery.min.js line 2 > eval:58:14 
0 error jquery-latest.min.js line 2 > eval:62:13 
Complete jquery-latest.min.js line 2 > eval:63:13 
OPTIONS XHR addressofserver/ 
XHR Object Object { readyState: 0, getResponseHeader: 
.ajax/v.getResponseHeader(), getAllResponseHeaders: 
.ajax/v.getAllResponseHeaders(), 

setRequestHeader: .ajax/v.setRequestHeader(), overrideMimeType:.ajax/v.overrideMimeType(), statusCode: .ajax/v.statusCode(), abort: .ajax/v.abort(), state:.Deferred/d.state(), always: .Deferred/d.always(), then:.Deferred/d.then(), 11 more… }

注意, “完成” 和 “出现了错误” 是,我在我的代码写的字符串。他们都指着Jquery。我知道jquery的链接正在工作,因为它抓取用户输入。

POST下

此外,它说,内容类型为text/html的,虽然我将它设置为JSON,否则它似乎好:

Vary: Accept-Encoding 
Server: Apache/2.4.10 (Ubuntu) 
Last-Modified: Fri, 22 Apr 2016 20:10:32 GMT 
Keep-Alive: timeout=5, max=100 
Etag: "c6c-531186b4ac855-gzip" 
Date: Fri, 22 Apr 2016 20:18:41 GMT 
Content-Type: text/html 
Content-Length: 1078 
Content-Encoding: gzip 
Connection: Keep-Alive 
Host: localhost 
Connection: keep-alive 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 

下选择它说

Connection: keep-alive 
Cache-Control: no-cache 
Access-Control-Request-Method: POST 
Access-Control-Request-Headers: content-type 
Accept-Language: en-US,en;q=0.5 
Accept-Encoding: gzip, deflate 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 

这也是打印此XHR对象时,我高亮它:

enter image description here

更新1:

我的其中一个按钮是刷新请求发送前的页面。现在,我得到一个不同的错误:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at */link*. (Reason: CORS header 'Access-Control-Allow-Origin' missing). 

我试图在添加这到$阿贾克斯:

 crossDomain: true, 
     xhrFields: { withCredentials: true }, 

我虽然还是得到同样的错误。

+0

检查网络选项卡中的devtools,看看是否被提出的要求以及它返回 –

+0

除此之外,没有'accept'选项在jQuery ajax中。 (*也许你想'accept',但应该是一个plainObject而不是一个字符串*)。此外,如果您的服务器永远不会超时并且出现问题,那么您将不会收到任何信息(*您可能想使用'timeout'选项*).. –

+0

“主要是它发送了一次或两次错误消息” ---哪些错误? – WillardSolutions

回答

0

东西,可以帮助你找出问题的根源:

  • 发送带有<form><input>普通帖子的请求,并检查服务器
  • 打开浏览器开发工具的 响应,看看如果u得到 什么奇怪的
  • 调试的实际概率的任何状态

还尝试改变这里的$.ajax通过$.post 参见方法文档: https://api.jquery.com/jquery.post/

相关问题