Pass Post Data with Window.Location.Href

Pass data without the use of window.location

The answer is yes. And the way to do it is to use a FORM. In particular, you should learn about the FORM method (learn about [REST][1] commands).

It sounds like you should be using POST.

[1]: https://www.restapitutorial.com/lessons/httpmethods.html#:~:text=The%20primary%20or%20most%2Dcommonly,or%20CRUD)%20operations%2C%20respectively.

window.location.href with POST instead of GET (or equivalent effect)

The way I have always done this (with jquery) is this.

var $form=$(document.createElement('form')).css({display:'none'}).attr("method","POST").attr("action","URLHERE");
var $input=$(document.createElement('input')).attr('name','FIRST NAME HERE').val("FIRST VALUE HERE");
var $input2=$(document.createElemet('input')).attr('name','SECOND NAME HERE').val("SECOND VALUE HERE");
$form.append($input).append($input2);
$("body").append($form);
$form.submit();

Passing parameters using Window.location.href

$id = $scope.x.id ;
$Name=$scope.x.Name;
$window.location.href = '/myfiles/test.php?id=' . $id . '&Name=' . $Name;

But you should look at POST method instead. It's safer and looks better (no clutter in the adress field)

http://www.w3schools.com/tags/ref_httpmethods.asp

How to pass variable to href in javascript?

You have to fetch field value using .value as you are passing whole object to the URL as document.getElementbyId('value') returns whole field object.

var dist = document.getElementById('value').value;

So your function should be like this

function check() {
var dist = document.getElementById('value').value; // change here
if (dist != "") {
window.location.href = "district.php?dist=" + dist;
} else
alert('Oops.!!');
}

how to send large data in location.href in java script

Please see here for limit of GET data. You can try to call that page by POST parameters.

Reference from gvee comment

How to pass params to Action Method from ASPX based view using window.location.href

Tested the code below in an ASP.NET MVC 5 application and it works fine.

Controller:

public class AccrualsController : Controller
{
public ActionResult Test(string YearAndMonth)
{
return View();
}
}

Home Index View:

<a id="btn" href="#" class="btn btn-primary">Click Me</a>

<script>

var btn = document.getElementById("btn");

btn.addEventListener("click", function (e) {
e.preventDefault();

window.location.href = "Accruals/Test?YearAndMonth=testvalue";
});

</script>

Suspect it may be something to do with the way ASP.NET MVC 1.0 is configured. Which is going to be hard to work out because ASP.NET MVC 1.0 is now over 10 years old, documentation is sparse and many people will have moved on to newer versions.

Bottom line

You are using an incredibly old version of ASP.NET MVC and should consider if possible upgrading to ASP.NET MVC 5, where the code you have shown will work



Related Topics



Leave a reply



Submit