Skip to content Skip to sidebar Skip to footer

Passing Values From Javascript To Servlet Not Working In Chrome

Im trying to pass parameters to servlet from javascript with : function selectHandler() { var selection = table.getChart().getSelection()[0]; var topping = data.getValue(select

Solution 1:

What you need is ajax call or say XMLHttpRequest as below:

<script type="text/javascript">
    function doAjax () {
        var request,
            selection = table.getChart().getSelection()[0],
            topping = data.getValue(selection.row, 0),
            answer=confirm("Delete "+topping+"?");

        if (answer && (request = getXmlHttpRequest())) {
            // post request, add getTime to prevent cache
            request.open('POST', "item?_method=delete&id="+topping+'&time='+new Date().getTime());
            request.send(null);
            request.onreadystatechange = function() {
                if(request.readyState === 4) {
                    // success
                    if(request.status === 200) {
                        // do what you want with the content responded by servlet
                        var content = request.responseText;
                    } else if(request.status === 400 || request.status === 500) {
                        // error handling as needed
                        document.location.href = 'index.jsp';
                    }
                }
            };
        }
    }
    // get XMLHttpRequest object
    function getXmlHttpRequest () {
        if (window.XMLHttpRequest
            && (window.location.protocol !== 'file:' 
            || !window.ActiveXObject))
            return new XMLHttpRequest();
        try {
            return new ActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
            throw new Error('XMLHttpRequest not supported');
        }
    }
</script>

You can also do it easily by jquery,

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" />
<script type="text/javascript">
    function doAjax () {
        ...
        $.ajax({
            url: "item?_method=delete&id="+topping+'&time='+new Date().getTime()),
            type: "post",
            // callback handler that will be called on success
            success: function(response, textStatus, jqXHR){
                // log a message to the console
                console.log("It worked!");
                // do what you want with the content responded by servlet
            }
        });
    }
</script>

Ref: jQuery.ajax()


Post a Comment for "Passing Values From Javascript To Servlet Not Working In Chrome"