Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net AngularJS$http.POST方法405错误_Asp.net_Angularjs_Asp.net Web Api_Cors - Fatal编程技术网

Asp.net AngularJS$http.POST方法405错误

Asp.net AngularJS$http.POST方法405错误,asp.net,angularjs,asp.net-web-api,cors,Asp.net,Angularjs,Asp.net Web Api,Cors,在$http.post方法中,我得到了405个问题。我们对POST和GET方法都使用单一服务(REST) 如果url具有本地主机,则它正在工作。urlAddScenario是localhost/Service.svc/api/scenarios/add。如果我使用机器名而不是locahost,它将不起作用。machinename/Service.svc/api/scenarios/add 我的JS代码是 scenarioExecutionFactory.addScenario = functi

在$http.post方法中,我得到了405个问题。我们对POST和GET方法都使用单一服务(REST)

如果url具有本地主机,则它正在工作。urlAddScenario是localhost/Service.svc/api/scenarios/add。如果我使用机器名而不是locahost,它将不起作用。machinename/Service.svc/api/scenarios/add

我的JS代码是

 scenarioExecutionFactory.addScenario = function (scenarioId) {
        return $http.post(urlAddScenario, scenarioId)               
    };
其他人:

 var runScenarioId = { "ScenarioId": 10 }
                   scenarioExecutionFactory.addScenario(runScenarioId )
                         .success(function (data) {
                             $scope.getScenarioRecentRuns($scope.CurrentScenario);
                         });
WCF服务:

  [OperationContract]
        [WebInvoke(UriTemplate = "api/scenarios/add",
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json,
            Method = "POST")]
        Request AddScenario(ScenarioRequestParams requestParams);
配置:

   <system.webServer>
<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Credentials" value="true" />
    <add name="Access-Control-Allow-Methods" value="GET,POST,PUT,OPTIONS" />
    <add name="Access-Control-Allow-Headers" value="Origin,Content-Type,Accept,Authorization,X-Ellucian-Media-Type" />
  </customHeaders>

</httpProtocol>
错误:

IE:SCRIPT7002:XMLHttpRequest:网络错误0x80070005,访问被拒绝

Chrome:XMLHttpRequest无法加载htp//machinename/services.svc/api/scenarios/add。无效的HTTP状态代码405


响应头:HTTP/1.1 405方法不允许

为了通过
web.config
正确启用
CORS
,您需要指定这3个自定义头

<httpProtocol>
  <customHeaders>
    <add name="Access-Control-Allow-Origin" value="*" />
    <add name="Access-Control-Allow-Headers" value="Content-Type" />
    <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" />
  </customHeaders>
</httpProtocol>

解决方案:

$http({
    url: "",
    method: "POST",
    data: $.param({
        data1: ""       
    }),
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }
});

我也面临着这个问题。获得405分,并做出选择。我在博客上发现angular编码参数的方式并没有被web服务器正确理解。因此,jQuery需要对POST数据进行编码

这是我的错误,我只需删除
method=“POST”action=“

坏的:

好:

检查此问题的答案您对ScenarioRequestParams的定义是什么?我的猜测是您以错误的方式构造了post请求。您是否真的在使用WeB API,因为您发布了来自ASMX WeB服务的代码。param是“ScenarioId”:10,它与request param无关。我在fiddler中测试了POST场景,它运行良好。从应用程序来看,它是如何工作的。您是否也通过配置来配置CORS?如果不是,你应该删除它。除此之外,您的Web API看起来像。问题应该出在你的防火墙上。我发布的web.config部分应该足够了。
$http({
    url: "",
    method: "POST",
    data: $.param({
        data1: ""       
    }),
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
    }
});