从JavaScript传递价值与Servlet不工作铬价值、工作、JavaScript、Servlet

2023-09-10 19:55:25 作者:若初遇

我试着来传递参数从JavaScript以与Servlet:

Im trying to pass parameters to servlet from javascript with :

function selectHandler() {
  var selection = table.getChart().getSelection()[0];
  var topping = data.getValue(selection.row, 0);
  var answer=confirm("Delete "+topping+"?");
  if(answer){
    document.location.href="/item?_method=delete&id="+topping;
    alert(topping+" has been deleted");
    location.reload();
  }
  else return false;
}

中的值越来越传递给servlet和工作正常,当我使用的火狐在我收到的网址为:的http:// XXXXXXX /项目_method =删除和放大器;?ID = XXXX 但是,当我使用的铬即发送该URL 的http:// XXXXXXX /条。作为值不会获得通过!我曾尝试与 window.location.href 还没有改变。可能是什么问题?

The values are getting passed to the servlet and is working fine when I'm using firefox as in I'm getting the url as: http://XXXXXXX/item?_method=delete&id=xxxx But when I'm using chrome the URL that is send is http://XXXXXXX/item. as the values are not getting passed!! I have tried with window.location.href also with no change. what could be the issue?

推荐答案

你需要什么是Ajax调用或者说XMLHtt $ P $如下pquest:

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>

您也可以做到这一点很容易的jQuery,

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>

参考: jQuery.ajax()