Ajax POST 方法无效 asp.net 核心
Ajax POST method not working asp.net core
我有 asp 个带有此控制器的网络核心应用程序:
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);
}
}
}
当我手动转到该页面时它可以工作并且页面中有 return json 字符串但是当我尝试从我的 js
文件中获取它时我的 ajax return 错误。这是我在获取它时使用的代码
$(function () {
$.ajax({
type: "POST",
url: "/Gallery/GetAll",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
},
error: function () {
alert("ERROR");
}
});
});
我假设您想要return 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");
}
});
});
有问题请告诉我
在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");
}
});
});
我有 asp 个带有此控制器的网络核心应用程序:
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);
}
}
}
当我手动转到该页面时它可以工作并且页面中有 return json 字符串但是当我尝试从我的 js
文件中获取它时我的 ajax return 错误。这是我在获取它时使用的代码
$(function () {
$.ajax({
type: "POST",
url: "/Gallery/GetAll",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
},
error: function () {
alert("ERROR");
}
});
});
我假设您想要return 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");
}
});
});
有问题请告诉我
在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");
}
});
});