简体   繁体   English

每次出错时都无法在ajax调用中获得成功响应

[英]Not getting success response in ajax call, every time going to error function

I am trying to get json data from servlet, from back-end I am getting proper response but every time call is going to error block of ajax call. 我正在尝试从servlet获取json数据,从后端我得到了正确的响应,但是每次调用都将转到ajax调用的错误块。 Here is My JS Code: 这是我的JS代码:

$(document).ready(function()

{
    $("#submit").on("click", function()
    {

        userId = $("#userNameTxt").val();
        seqAns = $("#seqAnsTxt").val();
        seqQues = $("#seqQues").val();
        command = $("#_CMD").val()
        alert(command);
        $.ajax({
            type : "POST",
            dataType : "json",
            url : "/Inventory/InvController",
            data : {_CMD : command, uid : userId, ques : seqQues, ans : seqAns},
            success : function(retVal)
            {
                alert("success");
                var test = $.parseJSON(retVal);
                if(true)
                {
                    $("#resetPwd").show();
                }
            },
            error: function(error)
            {
                alert("error");
            }
        });
    });
});

Here is My Servlet Code: 这是我的Servlet代码:

boolean isSuccess = logHelper.performForgotPwdValidation(agentDetails);

PrintWriter out = null;

    JsonObject jsonObj = new JsonObject();
    try 
    {
        jsonObj.addProperty("isValid", isSuccess);

        response.setContentType( "application/json" );

        response.setCharacterEncoding("UTF-8");
        out = response.getWriter();
        out.write(jsonObj.toString());
        out.flush();
    } 
    catch (IOException e) 
    {
        e.printStackTrace();
    }
    finally
    {
        if( null != out )
        {
            out.close();
        }
    }

While running every time it is going to the error function of the ajax call. 每次运行时,它将转到ajax调用的错误功能。

please help if I am doing anything wrong. 如果我做错任何事情,请帮忙。 Thanks in advance. 提前致谢。

Your data type is JSON, but your data is just a JavaScript object. 您的数据类型是JSON,但是您的数据只是一个JavaScript对象。 Use JSON.stringify() around the data so it's valid JSON data. 在数据周围使用JSON.stringify(),以便它是有效的JSON数据。 (Change to JSON.stringify({_CMD : command, uid : userId, ques : seqQues, ans : seqAns}) ) (更改为JSON.stringify({_ CMD:command,uid:userId,ques:seqQues,ans:seqAns})))

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

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