Asp.net mvc 4 ajax post与angularjs$http在使用ASP.NET MVC 4时不起作用

Asp.net mvc 4 ajax post与angularjs$http在使用ASP.NET MVC 4时不起作用,asp.net-mvc-4,angularjs,asp.net-ajax,Asp.net Mvc 4,Angularjs,Asp.net Ajax,我有两个项目客户端和服务器端。 客户端项目是纯htmljs。服务器端是ASP.NETMVC4和WebAPI。 因为有两个项目需要启用CRO功能。 我在服务器的网络配置中添加了: <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add nam

我有两个项目客户端和服务器端。 客户端项目是纯htmljs。服务器端是ASP.NETMVC4和WebAPI。 因为有两个项目需要启用CRO功能。 我在服务器的网络配置中添加了:

<system.webServer>
 <httpProtocol>
      <customHeaders>
        <add name="Access-Control-Allow-Origin" value="*" />
        <add name="Access-Control-Allow-Headers" value="Content-Type" />
      </customHeaders>
    </httpProtocol>
</system.webServer>
angular$http Post版本不工作:

$.post(url, appContext).done(
                function(data, textStatus, jqXHR) {
                    successFn(data, textStatus);
                })
                .fail(
                    function(jqXHR, textStatus, err) {
                        errorFn(err, textStatus);
                    });
 $http({
                url: url,
                method: 'POST',
                params: { appContext: appContext },
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                    /*'Accept': 'text/json'*/}
            }).success(successFn).error(errorFn);
var emsApp = angular.module('EmsWeb', ['ui.bootstrap']);
emsApp.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {

    $httpProvider.defaults.useXDomain = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
}]);
Request URL:http://localhost/EmsWeb/api/ModuleApi/GetModules?appContext=%7B%22globalDate%22%3A%22Mon%2C%2008%20Jul%202013%2013%3A09%3A35%20GMT%22%2C%22userToken%22%3A%22AlexToken%22%7D
Request Method:OPTIONS
Status Code:405 Method Not Allowed

Request Headers
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, origin, content-type
Access-Control-Request-Method:POST
Connection:keep-alive
DNT:1
Host:localhost
Origin:http://localhost:50463
Referer:http://localhost:50463/index.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
Query String Parametersview sourceview URL encoded
appContext:{"globalDate":"Mon, 08 Jul 2013 13:09:35 GMT","userToken":"AlexToken"}

Response Headers
Access-Control-Allow-Headers:Content-Type
Access-Control-Allow-Origin:*
Cache-Control:no-cache
Content-Length:76
Content-Type:application/json; charset=utf-8
Date:Mon, 08 Jul 2013 13:09:35 GMT
Expires:-1
Pragma:no-cache
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319
X-Powered-By:ASP.NET
当我使用$http时,会出现两个错误:

  • 选项url 405(不允许使用方法)
  • POST url 500(内部服务器错误)
  • NET MVC方法是

    [System.Web.Http.HttpPost]
    public List<Module> GetModules([FromBody]SessionContext appContext)
    {
     return  CreateModules();
    }
    
    我在浏览器中看到的选项请求标题:

    $.post(url, appContext).done(
                    function(data, textStatus, jqXHR) {
                        successFn(data, textStatus);
                    })
                    .fail(
                        function(jqXHR, textStatus, err) {
                            errorFn(err, textStatus);
                        });
    
     $http({
                    url: url,
                    method: 'POST',
                    params: { appContext: appContext },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                        /*'Accept': 'text/json'*/}
                }).success(successFn).error(errorFn);
    
    var emsApp = angular.module('EmsWeb', ['ui.bootstrap']);
    emsApp.config(['$routeProvider', '$httpProvider', function($routeProvider, $httpProvider) {
    
        $httpProvider.defaults.useXDomain = true;
        delete $httpProvider.defaults.headers.common['X-Requested-With'];
    }]);
    
    Request URL:http://localhost/EmsWeb/api/ModuleApi/GetModules?appContext=%7B%22globalDate%22%3A%22Mon%2C%2008%20Jul%202013%2013%3A09%3A35%20GMT%22%2C%22userToken%22%3A%22AlexToken%22%7D
    Request Method:OPTIONS
    Status Code:405 Method Not Allowed
    
    Request Headers
    Accept:*/*
    Accept-Encoding:gzip,deflate,sdch
    Accept-Language:en-US,en;q=0.8
    Access-Control-Request-Headers:accept, origin, content-type
    Access-Control-Request-Method:POST
    Connection:keep-alive
    DNT:1
    Host:localhost
    Origin:http://localhost:50463
    Referer:http://localhost:50463/index.html
    User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36
    Query String Parametersview sourceview URL encoded
    appContext:{"globalDate":"Mon, 08 Jul 2013 13:09:35 GMT","userToken":"AlexToken"}
    
    Response Headers
    Access-Control-Allow-Headers:Content-Type
    Access-Control-Allow-Origin:*
    Cache-Control:no-cache
    Content-Length:76
    Content-Type:application/json; charset=utf-8
    Date:Mon, 08 Jul 2013 13:09:35 GMT
    Expires:-1
    Pragma:no-cache
    Server:Microsoft-IIS/7.5
    X-AspNet-Version:4.0.30319
    X-Powered-By:ASP.NET
    
    有没有什么想法可以解释为什么角度法不起作用

    更新问题,因为事实证明它非常愚蠢: 因为我在中使用了参数和而不是数据:

     $http({
                    url: url,
                    method: 'POST',
                    params: { appContext: appContext },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                        /*'Accept': 'text/json'*/}
                }).success(successFn).error(errorFn);
    
    角度转换为GetRequest。也不需要ContentType。 所以实际代码是

     $http({method: 'POST',
                url: url,                
                data: appCtx,
            }).success(successFn).error(errorFn);
    
    所以几乎解决了,我看到angular仍然发出选项请求,但请求失败了,但post请求通过了


    因此,任何关于这一点的想法都是值得赞赏的

    如果您的ajax工作正常,那么看起来事情的角度方面没有配置。在使用$http(在控制器设置中)之前添加此选项

    有关工作示例,请参见此JSFIDLE:


    编辑:除了你的评论和对问题的编辑之外,这篇文章可能会帮助你:

    我的问题有两个原因: 我在中使用了参数和而不是数据

     $http({
                    url: url,
                    method: 'POST',
                    params: { appContext: appContext },
                    headers: {
                        'Content-Type': 'application/x-www-form-urlencoded'
                        /*'Accept': 'text/json'*/}
                }).success(successFn).error(errorFn);
    
    角度转换为GetRequest。也不需要ContentType。所以实际代码是

     $http({method: 'POST',
                url: url,                
                data: appCtx,
            }).success(successFn).error(errorFn);
    
    在服务器端 我需要一些东西来处理我的请求。一种方法是修饰方法w/[System.Web.Http.AcceptVerbs(“OPTIONS”)],我认为这不是最好的方法。 另一种方法是添加。
    我还在研究它…

    -确保get和Post的语法以及发送参数的方式
    -检查您在目标操作或方法中期望的参数,并确保发送和接收的参数相同。

    您使用的angularjs的哪个版本我的angularjs版本是1.0.7抱歉,我有,忘记发布了。在文章末尾添加。不是问题,伙计,这篇文章怎么样?它引用了一篇夜间文章,这篇文章的底部可能会帮助您改进CORS支持,而不仅仅是使用web。Config此链接描述了更细粒度的控制。我的问题是,即使是基本的angularjs请求也不起作用。。。