使用Javascript和REST登录

使用Javascript和REST登录,javascript,java,rest,Javascript,Java,Rest,我做了一个REST服务,如果登录为true,它将返回一个字符串“hej”。 我已经用一个rest客户机在Java中进行了测试,它工作得很好,但对javascript来说是个新手,需要一些帮助 我正在使用这个函数 function UserAction() { console.log(User()); var xhttp = new XMLHttpRequest(); xhttp.open("GET", "http://localhost:8080/Footballmanagerrestservi

我做了一个REST服务,如果登录为true,它将返回一个字符串“hej”。 我已经用一个rest客户机在Java中进行了测试,它工作得很好,但对javascript来说是个新手,需要一些帮助

我正在使用这个函数

function UserAction() {
console.log(User());
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "http://localhost:8080/Footballmanagerrestservice/webresources/login");
    xhttp.setRequestHeader("login", User());
    xhttp.responseType = 'text';

xhttp.onreadystatechange = function () {
    console.log('DONE', xhttp.readyState); 
    if (xhttp.readyState == 4) {;
        // handle response
        var response = xhttp.responseText;
        console.log(response);
        if (response == "hej") {
            var url = "http://localhost:8080/FM3/spil2.jsp";
            window.location.href = url;
        }
    }
};

// send the request *after* the callback is defined 
xhttp.send();
return false;
}

function User() {
username = document.getElementById("username").toString();
username = document.getElementById("password").toString();
var UserAndPass = "?username=" + username + "&password=" + password;
return UserAndPass;
} 
我向您展示我的客户端I-Java,也许您可以了解它为什么不工作

public static void main(String[] args) {
   Client client = ClientBuilder.newClient();

  String root="http://localhost:8080/Footballmanagerrestservice/webresources/";


String functionPath="login";

String parameters="?username=s153518&password=holger";
Response res = client.target(root+functionPath+parameters)
        .request(MediaType.APPLICATION_JSON).get();
String svar = res.readEntity(String.class);
System.out.println(svar);
}

代码的第一部分看起来还可以,但以下内容必须在函数内部处理,因为它本质上是异步的

var response = JSON.parse(xhttp.responseText);
console.log(response);
if (response.toString() == "hej") {
    var url = "http://localhost:8080/FM3/spil2.jsp";
    window.location.href = url
}
return false;
文件:

实际上,您试图将响应作为同步调用来处理,但事实并非如此,响应不是立即可用的,因此您必须注册一个回调(来自文档的回调必须附加到字段
onreadystatechange
),该回调将在服务器响应可用时由javascript触发

试着这样改变它:

  xhttp.onreadystatechange = function() {
    if (xhttp.readyState == 4) {
      // handle response
      var response = JSON.parse(xhttp.responseText);
      console.log(response);
      if (response.toString() == "hej") {
        var url = "http://localhost:8080/FM3/spil2.jsp";
        window.location.href = url
      }
    }
  }

  xhr.send();

您可以用java指定用户名和密码,但要用javascript登录。什么是User()?@AhmadWabbi User函数被添加:-)我仍然处于学习状态,对JavaScript和REST还很陌生。你能更深入地解释一下你的意思吗?好吧,我试着修改代码,结果发现两个错误。获取404(未找到)你知道它为什么找不到它吗?java客户机工作正常。下一个错误Uncaught SyntaxError:expected token<在JSON中位于XMLHttpRequest.UserAction.xhttp.onreadystatechange(load_user.js:18)的JSON.parse()的位置0处位于HTMLFormElement.onsubmit(index_1_1.html:51)的UserAction(load_user.js:28)处修复此方法:xhttp.open(),最后删除false。关于您正在尝试调试的问题,请在回调中放置一个断点,并检查您是否有响应OK you Karim!现在几乎可以用了。最后一个问题,您知道为什么REST服务的用户名和密码都为null吗?没问题:)。检查dom并确保已使用id=“username”和id=“password”定义了这两个元素