1

I have asp net core application with this controller:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using MySql.Data.MySqlClient;
using NewsletterWebsiteSample.Models;
using Newtonsoft.Json;

namespace NewsletterWebsiteSample.Controllers
{
    public class GalleryController : Controller
    {
        private readonly IHostingEnvironment _hostingEnvironment;
        public GalleryController(IHostingEnvironment he)
        {
            _hostingEnvironment = he;
        }

        [HttpPost]
        public IActionResult GetAll()
        {
            ErrorViewModel em = new ErrorViewModel();
            List<string> list = new List<string>();
            string[] files = Directory.GetFiles(_hostingEnvironment.WebRootPath + "\\Uploads\\Images");
            foreach (string file in files)
                list.Add(Path.GetFileName(file));

            em.Message = JsonConvert.SerializeObject(list);
            return View("Empty", em);
        }
    }
}

and when i manually go to that page it works and return json string in page but when i try to get that from my js file my ajax return error. Here is code i use when getting it

$(function () {
    $.ajax({
        type: "POST",
        url: "/Gallery/GetAll",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            alert(data);
        },
        error: function () {
            alert("ERROR");
        }
    });
});
2
  • I dont get it. Your url is GetAll but your action method is Post ? And also you return view ? Commented Apr 29, 2019 at 9:35
  • Well i am new to this. So i guess my controller method is problem? Commented Apr 29, 2019 at 9:40

2 Answers 2

3

I assume that you want to return list of file name in json format ? So I will change your code into this

    [HttpGet]
    public IActionResult GetAll()
    {
        ErrorViewModel em = new ErrorViewModel();
        List<string> list = new List<string>();
        string[] files = Directory.GetFiles(_hostingEnvironment.WebRootPath + "\\Uploads\\Images");
        foreach (string file in files)
            list.Add(Path.GetFileName(file));

        return Json(list);
    }

And your ajax code

$(function () {
    $.ajax({
        type: "GET",
        url: "/Gallery/GetAll",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            console.log(data);
        },
        error: function () {
            alert("ERROR");
        }
    });
});

Please let me know if you have any problem

Sign up to request clarification or add additional context in comments.

Comments

1

in the mvc core, you should don't say dataType: "json". please type this :

$(function () {
    $.ajax({
        type: "GET",
        url: "/Gallery/GetAll",
        contentType: "application/json; charset=utf-8",

        success: function (data) {
            console.log(data);
        },
        error: function () {
            alert("ERROR");
        }
    });
});

2 Comments

Could you please be more specific why i should or shouldn't do that.
when I use dataType: "json", I have many errors in my project. after erasing correct all of them. I tested this lot of time. I don't know but I think default send was json.

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.