This is my controller,
public ActionResult ReturnMethodTest(int id)
{
string name = "John";
return Json( new {data=name});
}
I am trying to get data from this controller by using code below but I am getting .
Can you please tell me what am I doing wrong?
$.ajax({
url: @Url.Action("ReturnMethodTest", "HomeController"),
data: {
id: 5,
},
success: function (data) {
console.log(data);
}
});
@Url.Action
only returns the action url's string, without quotes around it.
You'll need to wrap that url in quotes.
Replace:
url: @Url.Action("ReturnMethodTest", "HomeController"),
With:
url: '@Url.Action("ReturnMethodTest", "HomeController")',
// ^ ^
Otherwise, the file returned to the client will contain:
url: /HomeController/ReturnMethodTest,
Which isn't valid JS, nor what you want. The replacement gives the following result:
url: '/HomeController/ReturnMethodTest',
Which is a perfectly valid JavaScript string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With