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:

<scripttype="text/javascript">functiondoAjax () {
        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='+newDate().getTime());
            request.send(null);
            request.onreadystatechange = function() {
                if(request.readyState === 4) {
                    // successif(request.status === 200) {
                        // do what you want with the content responded by servletvar content = request.responseText;
                    } elseif(request.status === 400 || request.status === 500) {
                        // error handling as neededdocument.location.href = 'index.jsp';
                    }
                }
            };
        }
    }
    // get XMLHttpRequest objectfunctiongetXmlHttpRequest () {
        if (window.XMLHttpRequest
            && (window.location.protocol !== 'file:' 
            || !window.ActiveXObject))
            returnnewXMLHttpRequest();
        try {
            returnnewActiveXObject('Microsoft.XMLHTTP');
        } catch(e) {
            thrownewError('XMLHttpRequest not supported');
        }
    }
</script>

You can also do it easily by jquery,

<scriptsrc="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" /><scripttype="text/javascript">functiondoAjax () {
        ...
        $.ajax({
            url: "item?_method=delete&id="+topping+'&time='+newDate().getTime()),
            type: "post",
            // callback handler that will be called on successsuccess: function(response, textStatus, jqXHR){
                // log a message to the consoleconsole.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"