开发者

how to compare response.Text with Some String in ajax

开发者 https://www.devze.com 2023-03-03 21:11 出处:网络
I have a problem, I\'m returning data from a servlet to an AJAX function and in this function I want to compare the response.Text with some String, e.g x, but it does not compare it. I have the follow

I have a problem, I'm returning data from a servlet to an AJAX function and in this function I want to compare the response.Text with some String, e.g x, but it does not compare it. I have the following code;

function ajaxhandler()
{
    tableHtml=response.Text
    if(tableHtml=='true')
        alert("Valid Move");
    else
        alert("invalid move")
}

but this doesnt alert anything.

in my servlet i had done this;

void myfunction(HttpServlet request,HttpServlet response)
{
String user=(session.getAttribute("user"));
if(user=="john")
out.println("true");
else
out.println("false");
}
开发者_JS百科


Please post the result of console.log(JSON.Stringify(response));. I believe you are not sending a correct JSON response. Try this instead:

JSONObject jsonResponse = new JSONObject();
if(user.equals("john"))
    jsonObject.put ("Text", "true");
else
    jsonObject.put ("Text", "false");
out.println(jsonObject.toString());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号