简体   繁体   English

JSP向ajax调用返回值

[英]JSP to return value to ajax call

I want that jsp page that i called on ajax call to return a flag value 0 or 1 depending on my jsp code part.So to get that value I did something like this in ajax call: 我希望我在ajax调用中调用的jsp页面返回一个标志值0或1,具体取决于我的jsp代码部分,因此为了获得该值,我在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");
        }
        });
 });

And in the jsp part i only did : 在jsp部分中,我只做了:

<%
    if(flag==true){

    out.println(flag);
    }

    else if(flag==false){

    out.println(flag);

    }%>

How to get this value in my javascript ajax call.I want to store this value as i need to make decision based on this value. 如何在我的javascript ajax调用中获取此值。我想存储此值,因为我需要根据此值进行决策。

At present the msg that is being displayed on ajax success is : 目前,成功显示在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>

Return a json from the JSP page. 从JSP页面返回一个json。

Set the response as json.. 将响应设置为json。

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

In the AJAX success get the boolean value from json and use it... 在AJAX成功中,从json获取布尔值并使用它。

Update: 更新:

Return the flag value in a div and use that div id to get the flag value. div返回flag值,并使用该div id来获取标志值。

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

In the javascript use like this... 在像这样的javascript中使用...

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

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

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