0

I have a HomeController in my ASP.NET MVC application in folder "Controllers". My View is in: "View/Home/Index.cshtml" (look at my figure below).

I am using Ajax to get some json file every a few second. Problem is in Ajax URL, because I really don't know and didn't find, how to tell that property, that it has to go back a few folders and then find the HomeController.

My Solution looks like this:

Solution of my MVC Application

Here is a method in my HomeController:

[HttpGet]
public ActionResult GetRandomFeed()
{
    Item i = ss.getRandomFeed();
    return Json(new { Source = i.Media.Source, Avatar = i.User.Avatar, Text = i.Text, Name = i.User.Name }, JsonRequestBehavior.AllowGet);
}

My AJAX in the View:

setInterval(function () {
        $.ajax({
            type:        "GET",
            url:         '/HomeController.cs/GetRandomFeed', // Of course I have tried a lots of attempts in here
            contentType: "application/json;", // Not sure about this
            dataType:    "json",
            success: function (response) {
                console.log("Success :)");
            },
            error: function() {
                console.log("Error!");
            }
        });
    }, 2000);

All I want to get that JSON file (or can be even an array of strings) and use it in my Success function. It is a simple Slide Show and JSON contains the URLs that I want to show in the page every X seconds (just changing source of an image that is in that JSON file).

I couldn't find anything like this. How to use that URL correctly OR found something similiar for WebForms but cannot use it in MVC.

6
  • 2
    change url to '/Home/GetRandomFeed' Commented May 30, 2017 at 11:33
  • 2
    url: '/Home/GetRandomFeed' use this Commented May 30, 2017 at 11:33
  • 4
    url: '@Url.Action("GetRandomFeed", "Home")' Commented May 30, 2017 at 11:35
  • Thank you guys. The Home fixed the problem. The '@Url.Action("GetRandomFeed", "Home")' I didn't try, but as I can see, it is very similiar. Very simple and yet, couldn't find the mistake. Commented May 30, 2017 at 11:38
  • 1
    @Stepan You should read up on how the .NET MCV framework is used, especially in regard to controllers and actions. Your problem is elementary and you will run into more stuff like this if you do not do proper preparations. Commented May 30, 2017 at 11:43

2 Answers 2

2

Change your AJAX URL declaration to:

url: '/Home/GetRandomFeed' 

Remove the .cs

Or you can also do, assuming this view is under your controller:

url: '@Url.Action("GetRandomFeed")'
Sign up to request clarification or add additional context in comments.

3 Comments

Its /Home/.. not /HomeController/..
The: url: '/HomeController/GetRandomFeed' didn't work. But thanks anyway. It works with the "Home" word like in comments in first post.
Ooops sorry. I am used to Url.Action. I updated my code @StephenMuecke. Kindly check my updated answer. Thanks!
0

In my experience, it doesn't seem enter the function is just because the JSON return from the controller doesn't include Status = "OK"

[HttpGet]
public ActionResult GetRandomFeed()
{
...
...
return Json(new
            {
                Status = "Ok", ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.