Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
39
rated 0 times [  43] [ 4]  / answers: 1 / hits: 46968  / 9 Years ago, mon, july 13, 2015, 12:00:00

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 Syntax .



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);
}
});

More From » jquery

 Answers
5

@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.


[#65834] Friday, July 10, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
harleyaleenag

Total Points: 678
Total Questions: 121
Total Answers: 105

Location: Papua New Guinea
Member since Thu, Jul 9, 2020
4 Years ago
harleyaleenag questions
Thu, May 5, 22, 00:00, 2 Years ago
Wed, Aug 19, 20, 00:00, 4 Years ago
;