Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

197
Views
ASP .NET Core MVC JQuery AJAX - problem with getting data to view via controller api using ajax

When the line getJSON is executed and there is routing to for example: https://localhost:44338/ArticleApi/GetNextArticles?id=12&quantity=2&filterName=Odziez

I get error:

Failed to load resource: the server responded with a status of 404 ()

Cannot find this page on localhost

GetNextArticles method in my ArticleApiController:

 [HttpGet("{id}/{count}/{filterName}")]
    public IEnumerable<Article> GetNextArticles(int id, int count, string filterName)
    {
        return _articleRepository.GetNextArticles(id, count, filterName);
    }

in my view (.cshtml file):

 $.getJSON('@Url.Action("GetNextArticles", "ArticleApi")' + "?id=" + last + "&quantity=" + quantity + "&filterName=" + filterCategory, function(data, status) {
     
                    for(var index in data) {etc...}}

GetNextArticles method:

public IEnumerable<Article> GetNextArticles(int id, int count, string filterName)
    {
        List<Article> articles = new List<Article>();

        var categoryId = _context.Categories.Where(c => c.CategoryName == filterName).Select(c => c.CategoryId).FirstOrDefault();

        for (int i = 0; i < count; i++)
        {


            var foundArticles = _context.Articles.Where(a => a.CategoryId == categoryId && a.ArticleId == id + i + 1).ToList();

            if (foundArticles.Count() == 0)
            {
                return articles;
            }

            var article = foundArticles[0];

            if (article != null)
            {

                article.Category = _context.Categories.Find(article.CategoryId);

                articles.Add(article);
            }
            else
            {
                var nextArticles = _context.Articles.Where(a => a.CategoryId == categoryId && article.ArticleId <= id + i + 100).ToList();

                if (nextArticles.Count != 0)
                {
                    id = nextArticles.Min(a => a.ArticleId);

                    articles.Add(nextArticles.Where(a => a.ArticleId == id).First());
                }
                else
                {
                    break;
                }
            }
        }

        return articles;
    }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

According to the error, you could validate the names of the parameters that you are sending. Reviewing the code I realize that one of them is sent with the name "quantity" and in the controller you call it "count"

about 4 years ago · Juan Pablo Isaza Report

0

If you use [HttpGet("{id}/{count}/{filterName}")] in your Api, The Url passed into this api must be https://localhost:44338/ArticleApi/GetNextArticles/Id/count/filterName.

So, If you don't want to change the $.getJSON(..){...} in the View, You can change the api like:

    [HttpGet]
    public IEnumerable<Article> GetNextArticles([FromQuery]int id, int count, string filterName)
    {
        return _articleRepository.GetNextArticles(id, count, filterName);
    }

You can see the Api works fine and it can be loaded in Swagger as well

enter image description here enter image description here

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!