gpt4 book ai didi

c# - AngularJS 与 Asp.net Web API : $http post returning XMLHttpRequest cannot load: Response for preflight has invalid HTTP status code 405

转载 作者:可可西里 更新时间:2023-11-01 08:37:01 27 4
gpt4 key购买 nike

当尝试使用 $http 将 json POST 到 Asp.net web API 服务器时,它返回以下错误

XMLHttpRequest cannot load http://localhost:62158/api/video/add.
Response for preflight has invalid HTTP status code 405

但是从 $.ajax 发出相同的请求是工作文件。

$HTTP代码

$http.post(url, data, config)
.success(function (data, status, headers, config) {
defered.resolve(data);
})
.error(function (data, status, header, config) {
defered.reject(data);
});

$.ajax代码

$.ajax({ type: "POST",
url: url,
data: newVideo,
success: function (a) {
debugger;
},
error: function (e) {
debugger;
},
dataType: 'json'
});

asp.net web api代码和配置

web.config

<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>

WebApiConfig

public static void Register(HttpConfiguration config)
{
// Web API configuration and services

// Web API routes
config.MapHttpAttributeRoutes();

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);


VideoLIbraryDb.Config.setconnection();

var formatters = GlobalConfiguration.Configuration.Formatters;

formatters.Remove(formatters.XmlFormatter);

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));


}

API Controller

[RoutePrefix("api/video")]
public class VideoController : ApiController
{
[Route("add")]
[HttpPost]
public HttpResponseMessage add([FromBody] db_videos newVideo)
{
HttpStatusCode statusCode = HttpStatusCode.NotFound;

CommonResult result = new CommonResult() /// default
{
code = ResultCodes.retry.ToString(),
message = "Not able to complete request. Retry."
};

if (newVideo.video_file_name == null)
return Request.CreateResponse(statusCode, result);

if (newVideo.video_file_name.Length < 1)
return Request.CreateResponse(statusCode, result);


if (newVideo.insert())
{
statusCode = HttpStatusCode.OK;
result.code = ResultCodes.successfull.ToString();
result.message = "Video is added";
}

return Request.CreateResponse(statusCode, result);
}
}

最佳答案

@Rakeschand 你是对的,这是 cors

的问题

科尔斯

我使用 nu-get 命令行在我的项目中安装了 Cors

Install-Package Microsoft.AspNet.WebApi.Cors

并在 App_Start 文件夹中的 WebApiConfig.cs 文件中添加了以下代码。

var enableCorsAttribute = new EnableCorsAttribute("*",
"Origin, Content-Type, Accept",
"GET, PUT, POST, DELETE, OPTIONS");
config.EnableCors(enableCorsAttribute);

并从网络配置中删除了以下内容

   <remove name="X-Powered-By" />
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Headers" value="Accept, Content-Type, Origin" />
<add name="Access-Control-Allow-Methods" value="GET, PUT, POST, DELETE, OPTIONS" />

$http 开始像 $.ajax 一样工作

但这些事情让我有些困惑。如果有人能详细说明,我会很饱

  1. 为什么 $.ajax 有效而 $http 无效
  2. 我通过 cors 做了同样的事情,我在 web.config 中做了同样的事情那么为什么 cors 工作但 web.配置没有?

关于c# - AngularJS 与 Asp.net Web API : $http post returning XMLHttpRequest cannot load: Response for preflight has invalid HTTP status code 405,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37629422/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com