0

I don't know if it is possible or not. I referred some site, but I didn't get exact answer.

I am using 
<a href="xyz?id=4"> click </a>

When I send this request to server in the response page easily I can see "id=4" in address bar, obviously which is not secure, But in post request we cant see this.

So can we convert a get request to post or ant other way is there to hide this from address bar.

Thanks in advance.

Mickey Patel
  • 501
  • 1
  • 4
  • 16

3 Answers3

4

Firstly, to convert GET to POST, simply change the link to a form:

<form id="myForm" action="xyz" method="post">
  <input type"hidden" name="id" value="4"/>
</form>

This form will not be visible and you can easily auto-submit it using JavaScript in your link:

<a href="javascript:void document.getElementById('myForm').submit();"> click </a>

Secondly and more importantly, both GET and POST are equally not secure over HTTP. To secure them, use HTTPS and they will be both equally secure, so no need to change if GET is working for you.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55
1
<a href="#" onclick="postForm()"> click </a>

Dynamically create a from and post it.

function postForm() {
var form = $('<form method="POST" action="xyz"></form>');
$(document.body).append(form);
form.append('<input type="hidden" name="id" value="4"/>');
form.submit();
}

As Racil suggested in comments, you can also do the following

<a href="#" id="postLink"> click </a>

and then

$('#postLink').click(function(e){
   e.preventDefault();
   //create form and post
});
Anupam
  • 7,966
  • 3
  • 41
  • 63
  • Nice, but I wouldn't use both `href` and `onclick`. I'd use `click`. – Racil Hilan Jun 02 '16 at 06:25
  • @RacilHilan yes or bind anchor's id to bind click event with `href='#'` – Anupam Jun 02 '16 at 06:26
  • @anu don't know why but $(document).append(form); line is not working. – Mickey Patel Jun 02 '16 at 06:45
  • @MickeyPatel Try `$("body").append(form)` or `$(document.body)` – Anupam Jun 02 '16 at 06:49
  • What you told that is working fine, but after redirecting the response to a page when I am trying to hit the enter button in address bar it is sowing me this error "HTTP Status 405 - Request method 'GET' not supported" @anu – Mickey Patel Jun 02 '16 at 13:18
  • Looks like some [spring related problem](http://stackoverflow.com/q/3333611/456135), which I am not good at. Try [this](http://stackoverflow.com/q/3333611/456135) or post a separate question – Anupam Jun 02 '16 at 18:40
0

Call a java script function on onclick which will make the form submission using post method or you can use ajax call to post the data and get your desired results.Use id as a parameter in function.

<a href="#" onclick="postData(4)">

/// Javascript function for ajax call
function postData(id){
  var param = { "Id": id};
  $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    url: "xyz.aspx",
    data: JSON.stringify(param),
    success: function (data) {
         /// Recive data here or do your stuff here
    }
}

Make a form having single input type hidden and onclick set value of that input type hidden element and submit form using jquery.

<form id="target" action="destination.html">
   <input type="hidden" id="hiddenValue">
</form>

/// Javascript function for setting value of hidden element and form submission using jquery
    function postData(id){
       $("#hiddenValue").val(id);
       $("#target").submit();
    }

Hopefully this will solve your problem.

Asad
  • 498
  • 7
  • 21