代码之家  ›  专栏  ›  技术社区  ›  Poorna Senani Gamage

如何将参数绑定到URL

  •  1
  • Poorna Senani Gamage  · 技术社区  · 6 年前

    这是我的jsp代码。我想把 卡蒂 调用时url的参数 getproductsub() 并发送ajax请求。

    r.open("GET", "url?catid=" + catid,true);
    

    上面这行代码似乎有错误。我该如何修复它。

    <select class="form-control" id="pcategory" name="pcategory" onchange="getproductsub();">
    
    <script>
      function getproductsub() {
        var catid = document.getElementById('pcategory').value;
        var url = window.location.href;
        var r = new XMLHttpRequest();
        r.onreadystatechange = function() {
          if (r.readyState === 4 && r.status === 200) {}
        };
        r.open("GET", "url?catid=" + catid, true);
        r.send();
      }
    </script>
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Roshana Pitigala Laxmansinghsodhanohdiyala    6 年前

    请注意 window.location.href 提供完整的位置,包括当前时间url中的参数。所以 ? 可能包括在里面。

    你可以使用

    var url = window.document.location.pathname;
    

    以及 url 传递给的字符串 open() .

    r.open("GET", url + "?catid=" + catid, true);
    

    或者

    使用 getRequestURL() 在jsp中编写代码时。

    r.open("GET", "<%= request.getRequestURL()%>?catid=" + catid, true);