简体   繁体   English

URL太长-如何在jquery中为锚标记执行POST

[英]Too long URL - How to do POST in jquery for anchor tag

In my application, I'm using JQuery data table to show my product details. 在我的应用程序中,我正在使用JQuery数据表来显示我的产品详细信息。 I've lot of purchase filters(price, product type etc.) and finally in data table users have link to view the selected product details. 我有很多购买过滤器(价格,产品类型等),最后在数据表中,用户具有查看所选产品详细信息的链接。 The schematic diagram of table is as below; 表的示意图如下:

表

While table population, I've appended filter params to the href of anchor element. 在进行表格填充时,我将过滤器参数附加到了锚元素的href中。 The code snippet is as below; 代码段如下:

"mRender" : function(page, type, full) {
        return '<a href="' + CXT_PATH
                + '/page?filters='
                + getFilters()
                + '">' + "LINK" + '</a>'
                }                                       
                }

Here getFilters() some times returns 100's of filters, so the url created is too long. 这里的getFilters()有时会返回100个过滤器,因此创建的网址太长。 I think, while default anchor link click they are passed as GET request. 我认为,当默认锚链接单击时,它们将作为GET请求传递。 So there could be a size limit. 因此可能会有大小限制。

So I don't want my users to get weird results, when they selected more filters.. 所以我不希望我的用户在选择更多过滤器时得到奇怪的结果。

I'm planning to do as below: 我打算做如下:

Whenever user clicks the link, it will invoke a method in javascript and do form post (because page reloading is required). 每当用户单击链接时,它将调用javascript中的方法并进行表单发布(因为需要重新加载页面)。 Please guide me, how to achieve that in javascript or jquery OR any other better ways to achieve the same. 请指导我,如何使用javascript或jquery或任何其他更好的方法来实现这一目标。

I'm using Spring MVC + JSP + Javascript & JQuery 我正在使用Spring MVC + JSP + Javascript和JQuery

Here is a start: 这是一个开始:

Using the XMLHttpRequest object: 使用XMLHttpRequest对象:

Source for the getXHR function: XMLHttpRequest Browser Support getXHR函数的源代码: XMLHttpRequest浏览器支持

function getXHR() { 
  if (window.XMLHttpRequest) {
    // Chrome, Firefox, IE7+, Opera, Safari
    return new XMLHttpRequest(); 
  } 
  // IE6
  try { 
    // The latest stable version. It has the best security, performance, 
    // reliability, and W3C conformance. Ships with Vista, and available 
    // with other OS's via downloads and updates. 
    return new ActiveXObject('MSXML2.XMLHTTP.6.0');
  } catch (e) { 
    try { 
      // The fallback.
      return new ActiveXObject('MSXML2.XMLHTTP.3.0');
    } catch (e) { 
      alert('This browser is not AJAX enabled.'); 
      return null;
    } 
  } 
}


var xhr = getXHR();
xhr.open("POST", url, true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.send("your-querystring");

you could try the following method, create a form in table and when clicking on filter link assign the filter value to hidden element in form and submit the form. 您可以尝试以下方法,在表格中创建表单,然后在单击过滤器链接时将过滤器值分配给表单中的隐藏元素,然后提交表单。

HML HML

<form action="<server_path>" method="post" id="filterform">
<input type="hidden" name="filters" id="filters" />
<input type="submit/>
</form>

Javascript/ Jquery Javascript / Jquery

"mRender" : function(page, type, full) {
        return '<a onclick="' submitFilter() '">' + "LINK" + '</a>'
 }     

submitFilter: function (CXT_PATH){
       $( "#filters" ).val(getFilters());
       $( "#filterform" ).submit();
}

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

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