【问题标题】:JSP to return value to ajax callJSP将值返回给ajax调用
【发布时间】:2014-04-22 05:27:50
【问题描述】:

我希望我在 ajax 调用中调用的那个 jsp 页面根据我的 jsp 代码部分返回一个标志值 0 或 1。所以为了获得该值,我在 ajax 调用中做了这样的事情:

<script type="text/javascript">
        $(document).ready(function () {
        $('#photo').photobooth().on("image", function (event, dataUrl) {
        alert(dataUrl); 
        //alert($('#mygroupuserid'));
        //alert(document.Clickpictures.OwnerId.value);
        //alert(imgdata);
        $.ajax({
            url: 'uploadwebcamimage.jsp',
            type: "POST",

            data: {
                encodeimg: dataUrl,
                OwnerId: document.Clickpictures.OwnerId.value,
                OwnerPhone: document.Clickpictures.OwnerPhone.value,
                mobilepass: document.Clickpictures.mobilepass.value,
                emailpass: document.Clickpictures.emailpass.value,
                mypassword: document.Clickpictures.mypassword.value,
                mygroupuserid: document.Clickpictures.mygroupuserid.value

            },
            error : function(){ 
                alert('Error'); 
            },
            success: function(msg){
                //alert((msg));
                    $('#message').html(msg);
            }
        });

        $("#gallery").show().html('<img src="' + dataUrl + '" >');
        var value=document.getElementById("message").innerHTML;
        alert(value);
        if(value==true){
            alert("Face Detection Success");
        }
        else if(value==false){
            alert("Face Detection Unsuccessful");
        }
        });
 });

在 jsp 部分我只做了:

<%
    if(flag==true){

    out.println(flag);
    }

    else if(flag==false){

    out.println(flag);

    }%>

如何在我的 javascript ajax 调用中获取此值。我想存储此值,因为我需要根据此值做出决定。

目前在ajax成功时显示的消息是:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>    
    true
</body>
</html>

【问题讨论】:

    标签: javascript jquery ajax jsp


    【解决方案1】:

    从 JSP 页面返回一个 json。

    将响应设置为 json..

    response.setContentType("application/json");
    response.setHeader("Content-Disposition", "inline");
    

    在 AJAX 成功中,从 json 中获取布尔值并使用它...

    更新:

    div 中返回flag 值并使用该div id 获取标志值。

    out.println("<div id='flag'>"+flag+"</div>");
    

    在javascript中这样使用...

    $("#message").find("#flag").html();
    

    【讨论】:

    • 实际上我认为它没有用。我只返回一个标志值,所以我将它设置在我的当前页面的一个 div tage 中,该页面正在像这样进行 ajax 调用:成功:函数(msg){ $('#message').html(msg); }但问题是如何在我的javascript中使用这个设置值?
    • 见我编辑了我的帖子。查看 ajax 调用的成功部分及其下方。我在 html 中使用 id=message 进行了划分。但是当我打印警报(值)时它返回空白。可能是什么原因?
    猜你喜欢
    • 1970-01-01
    • 2010-09-28
    • 2017-10-22
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多