i am trying to do a ajax get request in asp.net mvc 3. It does not work though ie the GetSquareRoot action is not hit?
index.cshtml
@{
ViewBag.Title = "Home Page";
}
<h2>@ViewBag.Message</h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">
http://asp.net/mvc</a>.
</p>
<script type="text/javascript">
function calculateSquareRoot(numberToCalculate) {
$.ajax({
type: 'Get',
url: '/Home/GetSquareRoot',
data: { number: numberToCalculate },
success: function (data) { alert(data.result); }
});
}
</script>
<button onclick="calculateSquareRoot(9);">Calculate Square</button>
on the homecontroller:
public JsonResult GetSquareRoot(long number)
{
var square = Math.Sqrt(number);
return Json(new { result = square }, JsonRequestBehavior.AllowGet);
}
If I mount the following page on localhost:
When I hit the button, I see a request issued to:
Are you sure you’ve told us the whole story?