ASP MVC jQuery $ .ajax POST请求不调用控制器方法,但在“新鲜”中工作MVC项目 [英] ASP MVC jQuery $.ajax POST request does not call controller method, but works in "fresh" MVC project

查看:142
本文介绍了ASP MVC jQuery $ .ajax POST请求不调用控制器方法,但在“新鲜”中工作MVC项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在ASP.NET MVC项目中,我有一个接受POST请求的控制器方法,如此(使用User类来完成):

In an ASP.NET MVC project, I have a controller method that accepts POST requests, like so (with the "User" class for completeness):

[HttpPost]
public ActionResult TestMethod(User user)
{
    return Content("It worked");
}

public class User
{
    public string Name { get; set; }
    public string Email { get; set; }
}

我用jQuery Ajax调用这个方法:

I call this method with jQuery Ajax:

$.ajax({
    url: '/test/TestMethod/',
    data: JSON.stringify({ user: { name: 'NewUserName', email: 'username@email.com' } }),
    type: 'POST',
    success: function (data) {
        alert(data);
    },
    error: function (xhr) {
        alert('error');
    }
});

当我创建一个全新的ASP.NET MVC项目,并将此代码包含在新的Test控制器中时,一切正常。通过Fiddler查看,发出了一个POST请求,然后我得到了控制器方法的返回值。

When I create a fresh ASP.NET MVC project, and include this code in a new Test controller, everything works fine. Viewed through Fiddler, a single POST request is made, and I get the controller method return value back.

然而,当我在我正在开发的当前MVC项目中运行此代码时,它不起作用。从Fiddler我看到ajax调用首先启动一个POST方法,得到301 http状态错误(永久移动?)。之后立即发出GET请求,这会产生404未找到错误(这是有道理的,因为没有可用此名称的GET操作方法)。

However, when I run this code in the current MVC project that I'm developing, it doesn't work. From Fiddler I see that the ajax call first initiates a POST method, that gets a 301 http status error ("moved permanently"?). Immeditely afterwards a GET request is made, which generates a 404 not found error (which makes sense as there is no GET action method with this name available).

所以我使用完全相同的代码,在新项目和现有项目中,但代码仅适用于新项目。很明显,我的现有项目有些东西会以某种方式阻止它正常运行(并导致产生POST和GET请求的奇怪行为)。但我不知道它可能是什么,所以任何建议都欢迎......!

So I use the exact same code, in a fresh projects and in the existing project, but the code only works in the fresh project. So clearly there's something about my existing project that somehow prevents this from running properly (and causes the odd behaviour of generating both a POST and a GET request). But I have no idea what it could be, so any suggestions welcome...!

更新 - 路线信息:

public static void RegisterRoutes(RouteCollection routes)
{
  routes.AppendTrailingSlash = true;
  routes.MapMvcAttributeRoutes();
  routes.MapRoute(
  name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });
}

更新2:
看起来这个问题是由内容安全引起的为此项目启用的策略设置。

Update 2: Looks like this issue is caused by Content Security Policy settings that were switched on for this project.

推荐答案

我在实现自定义错误后遇到了这个问题,而我的表单正在将文件发回服务器tthat更大比Max允许的。这导致404.13错误...我猜这是文件太大的默认错误

I ran into this issue after I implemented Custom Errors, and My form was posting a File back to the server tthat was larger than the Max Allowed. This resulted in a 404.13 error... I guess that is the default error for "file too large"

当打开自定义错误时,我看到的只是404。当我知道请求类型是一个帖子,并且发布的网址是正确的时,我发现404错误让我发疯。

When custom errors were turned on all I saw was the 404. It was driving me crazy that I was getting a 404 error when I knew that the request type was a post, and the url where it was posting was correct.

希望这有助于某人。

这篇关于ASP MVC jQuery $ .ajax POST请求不调用控制器方法,但在“新鲜”中工作MVC项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆