[英]Making href (anchor tag) request POST instead of GET as passing the parameter
The issue is that when I am using the below approach, the entire URL is showing up. 问题是,当我使用以下方法时,整个URL都会显示出来。 Maybe because of the GET request if href. 如果是href,则可能是因为GET请求。 I want to avoid this and and I want the POST request. 我想避免这种情况,并且我想要POST请求。
index.html 的index.html
<a id="ccviewMiniStatementId">View mini statement</a>
nonFinancial.js nonFinancial.js
$('#ccviewMiniStatementId').click(function(event) {
var ccMonthVal=$("#ccMonthId").val();
var ccYearVal=$("#ccYearId").val();
var cCRefNumId=$("#cCRefNumId").val();
var errExpVal = validateCcExpiry();
if (errExpVal==false)
{
event.preventDefault();
return false;
}
// I am passing values to my controller and there it is GET
//because POST is not supported. I want to make it POST.
$("#ccviewMiniStatementId").attr("href", "getpdfcreditcard?month="+ccMonthVal+"&year="+ccYearVal+"&cCReferenceNo="+cCRefNumId);
$("#pdfViewer").submit();
});
My controller NonFinancial.java 我的控制器NonFinancial.java
@RequestMapping(value="/getpdfcreditcard", method=RequestMethod.GET, params = {"month","year","cCReferenceNo"})
public ResponseEntity<byte[]> getPDF(@RequestParam(value="month", required = true) String month,
@RequestParam(value="year", required = true) String year,
@RequestParam(value="cCReferenceNo", required = true) String cCReferenceNo,
HttpServletResponse response) throws IOException, SQLException {
logger.debug("month/year/cCReferenceNo: "+month+"/"+year+"/"+cCReferenceNo);
}
So I would like: when I am clicking on an anchor tag it should do a POST. 所以我想:当我单击一个锚标记时,它应该执行POST。 How do I do that? 我怎么做? I don't want to use a <form></form>
我不想使用<form></form>
looks like you are using jQuery, so it is easy: 看起来您正在使用jQuery,因此很容易:
$('a').click(function() {
var fullLink = $(this).attr('href');
var splittedLink = fullLink.split("?");
var href = splittedLink[0];
var qs = splittedLink[1];
// convert qs to body
var splittedQs = qs.split('&');
var body = {};
for(var key in splittedQs) {
var t = splittedQs[key].split('=');
body[t[0]] = t[1];
}
$.ajax({
type: 'post',
data: body,
url: href,
success: function() { ... }
});
return false; // prevent default
});
It is not possible in HTML to make a href
use POST. 在HTML中,不可能使href
使用POST。 However as @modernator pointed out, you can use JavaScript to listen to the click event and then manually trigger an AJAX request. 但是,正如@modernator指出的那样,您可以使用JavaScript来监听click事件,然后手动触发AJAX请求。
Alternatively you could use a form with hidden inputs. 或者,您可以使用带有隐藏输入的表单。 In there you can specify the submit method. 您可以在其中指定Submit方法。 Example: 例:
<form method="post" action="<your-url>">
<input type="hidden" name="month" value="<month-value>">
<button type="submit">View Statement</button>
</form>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.