Aleksa Ristic
Aleksa Ristic

Reputation: 2499

Ajax POST method not working asp.net core

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

Upvotes: 1

Views: 4951

Answers (2)

LPLN
LPLN

Reputation: 473

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

Upvotes: 1

user4851087
user4851087

Reputation:

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

Upvotes: 3

Related Questions