简体   繁体   English

无法在Ajax响应中获得价值

[英]Unable to get value in Ajax response

The servlet which receives request -- 接收请求的servlet-

     Gson gson = new Gson(); 
    JsonObject myObj = new JsonObject();
    LoginBean loginInfo = getInfo(userId,userPwd);
    //System.out.println("00000000-----------"+loginInfo.userId);
    JsonElement loginObj = gson.toJsonTree(loginInfo);
    if(loginInfo.getUserId() == "GUEST!"){

        myObj.addProperty("success", false);
    }
    else {

        myObj.addProperty("success", true);
    }
    myObj.add("login", loginObj);
    System.out.println(":::::"+myObj.get("success"));
    out.println(myObj.toString());
    out.close();

Here is the js file ----- 这是js文件-----

function loadScript(url, callback)
{
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
script.onreadystatechange = callback;
script.onload = callback;
head.appendChild(script);
}
    loadScript("js/json2.js", myJSCode);
    var myJSCode = $(document).ready(function() {

$("#loginForm").submit(function(e){
       e.preventDefault();
});


$("#login").click(function(e){


        dataString = $("#loginForm").serialize();


        var id = $("input#id").val(); 
        var pwd = $("input#pwd").val();
        dataString = "id=" + id + "&pwd="+pwd;

        $.ajax({
            type: "POST",
            url: "login",
            data: {
                id : id,
                pwd : pwd
            },

            dataType: "json",


            success: function( data, textStatus, jqXHR) {
                 if(data.success){
                     $("#ajaxResponse").html("");
                     $("#ajaxResponse").append("<b>Welcome</b> " + data.login.id + "<br>");
                     //alert("#ajaxResponse" + data.login.id);
                     alert(data['id']);   
                     alert(data['pwd']);

                 } 

                 else {
                     $("#ajaxResponse").html("<div><b>Login Credentials are invalid!</b></div>");
                 }
            } 
 });     

I am getting the ajaxResponse if I am going with 1 element ie 'id' but when I tried to get both the response I am getting value as undefined. 如果我要使用1个元素(即“ id”),则会得到ajaxResponse,但是当我尝试同时获取两个响应时,我会得到未定义的值。 I have tried with data.login.id & data[id] but unable to get desired output. 我尝试使用data.login.id和data [id],但无法获得所需的输出。 Any help will be appreciated. 任何帮助将不胜感激。 Even I have tried with JSON.Stringify(). 即使我尝试了JSON.Stringify()。

     $.ajax({
                type: "POST",
                url: "login",
                data: {
                    id : id,
                    pwd : pwd
                },
...

This has data passed to ajax is not the same as the data in 传递给ajax的数据与in中的数据不同

    success: function( data, textStatus, jqXHR) {
                     if(data.success){
...

In this success callback data is response from the server and it has a different (or same structure) based on data that server returns to you. 在这种成功中,回调数据是服务器的响应,并且基于服务器返回给您的数据,回调数据具有不同的(或相同的结构)。 if you want to print the value from ajax call, you need to use just id 如果要打印来自ajax调用的值,则只需使用id

$("#ajaxResponse").append("<b>Welcome</b> " + id + "<br>");

I see you are trying to use "data.login.id" can you check what is real structure of data in response? 我看到您正在尝试使用“ data.login.id”,您能否检查响应中数据的真实结构? add console.log(data) or put breakpoint that callback, or simple add debugger; 添加console.log(data)或在断点处添加回调,或简单地添加调试器; code, it will stop code in that line for you without adding breakpoint. 代码,它将为您停止该行中的代码,而无需添加断点。

Thanks Alex. 谢谢亚历克斯。 I will give you +1 for the line 我给你+1

  $("#ajaxResponse").append("<b>Welcome</b> " + id + "<br>");

Though it is working when I use: 虽然在我使用时可以使用:

      data: {

                id: $('#id').val(),
                pwd: $('#pwd').val()
            },

Without changing my code to this one the line was not working. 没有将我的代码更改为这一行,该行将无法正常工作。 Thanks for the suggestion. 谢谢你的建议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM