【问题标题】:Ajax POST method not working asp.net coreAjax POST 方法不起作用 asp.net 核心
【发布时间】:2019-09-17 21:15:03
【问题描述】:

我有这个控制器的 asp net core 应用程序:

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

当我手动转到该页面时,它可以工作并在页面中返回 json 字符串,但是当我尝试从我的 js 文件中获取它时,我的 ajax 返回错误。这是我获取时使用的代码

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

【问题讨论】:

  • 我不明白。您的网址是 GetAll 但您的操作方法是 Post ?你也返回视图?
  • 好吧,我是新手。所以我猜我的控制器方法有问题?

标签: ajax asp.net-core asp.net-ajax


【解决方案1】:

在mvc核心中,你不应该说dataType: "json"。 请输入:

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

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

【讨论】:

  • 能否请您更具体地说明为什么我应该或不应该这样做。
  • 当我使用dataType: "json" 时,我的项目中有很多错误。删除正确的所有这些后。我测试了很多时间。我不知道,但我认为默认发送是 json。
【解决方案2】:

我假设您想以 json 格式返回文件名列表?所以我会把你的代码改成这个

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

还有你的 ajax 代码

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

如果您有任何问题,请告诉我

【讨论】:

    猜你喜欢
    • 2012-05-31
    • 1970-01-01
    • 2021-07-22
    • 2019-07-29
    • 2018-10-17
    • 2018-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多