Reputation: 627
In my ASP application I'm trying to retrieve a value from the database. From javascript I try to execute a controller method.
Javascript
function getCapital(country) {
//alert(country);
var url = "~/Home/Capital";
$.get(url, { country: country }, function (response) {
$("#getCapital").html(response);
alert(response);
});
Controller (Home)
public string Capital(string country)
{
return capitalService.getCapital("Frankrijk");
}
But I get no result.
EDIT
View
<script type="text/javascript" src="/Scripts/jquery-1.4.1.js"></script>
<script type="text/javascript" src="/Scripts/World.js" ></script>
This the output of Browser Console. So sometimes it logs the URL without an error.
Upvotes: 0
Views: 87
Reputation: 51
Issue is seems to be with your URL, in javascript you do not need to put '~' with your URL, you can use
var url = "/Home/Capital";
The better approach for URL in MVC is to use @URL by passing controller and action name as below
@Url.Action("Capital", "Home")
So you can modify your javascript as below, put a breakpoint in your action "Captial" and see you get a call, you can also look into the browser console and see if any error occurs.
function getCapital(country) {
//alert(country);
var url = '@Url.Action("Capital", "Home")';
console.log(url);
$.get(url, { country: country }, function (response) {
$("#getCapital").html(response);
alert(response);
});
}
EDIT OR
function getCapital(country) {
//alert(country);
var url = "/Home/Capital";
console.log(url);
$.get(url, { country: country }, function (response) {
$("#getCapital").html(response);
alert(response);
});
}
Let me know if you need more information.
Upvotes: 1