C# 带有Asp.net Web API的AngularJS:$http post返回XMLHttpRequest无法加载:飞行前的响应具有无效的http状态代码405
尝试使用C# 带有Asp.net Web API的AngularJS:$http post返回XMLHttpRequest无法加载:飞行前的响应具有无效的http状态代码405,c#,asp.net,angularjs,asp.net-web-api,asp.net-web-api2,C#,Asp.net,Angularjs,Asp.net Web Api,Asp.net Web Api2,尝试使用$http将json发布到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
$http
将json发布到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({ type: "POST",
url: url,
data: newVideo,
success: function (a) {
debugger;
},
error: function (e) {
debugger;
},
dataType: 'json'
});
$.ajax代码
$http.post(url, data, config)
.success(function (data, status, headers, config) {
defered.resolve(data);
})
.error(function (data, status, header, config) {
defered.reject(data);
});
$.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>
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控制器
[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);
}
}
[RoutePrefix(“api/视频”)]
公共类视频控制器:ApiController
{
[路线(“添加”)]
[HttpPost]
公共httpresponsemessageadd([FromBody]db_videos newVideo)
{
HttpStatusCode statusCode=HttpStatusCode.NotFound;
CommonResult=新建CommonResult()///默认值
{
code=ResultCodes.retry.ToString(),
message=“无法完成请求。请重试。”
};
if(newVideo.video\u文件名==null)
返回请求.CreateResponse(状态码、结果);
如果(newVideo.video\u file\u name.Length<1)
返回请求.CreateResponse(状态码、结果);
if(newVideo.insert())
{
statusCode=HttpStatusCode.OK;
result.code=ResultCodes.successfull.ToString();
result.message=“已添加视频”;
}
返回请求.CreateResponse(状态码、结果);
}
}
@Rakeschand你是对的,这是cors的问题
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);
并从web配置中删除以下内容
<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
正在工作
但是这些事情让我有些困惑。如果有人能详细说明,我会非常满意
$http.post(url, data, config)
.success(function (data, status, headers, config) {
defered.resolve(data);
})
.error(function (data, status, header, config) {
defered.reject(data);
});
$.ajax({ type: "POST",
url: url,
data: newVideo,
success: function (a) {
debugger;
},
error: function (e) {
debugger;
},
dataType: 'json'
});
$.ajax
工作而$http
不工作cors
做了同样的事情,我在web.config
中做了同样的事情,那么为什么cors
工作了,但是web.config
没有我认为您必须在ajax调用中设置内容类型:
contentType: 'application/x-www-form-urlencoded; charset=utf-8',
或
在后端应用cors不知道怎么做。请指导@rakescandit这是一个cors问题,需要从后端asp.net端解决。你可以搜索它。我也不知道asp。我遇到了同样的问题,这帮我解决了。我很想了解为什么web.config不起作用。为什么web.config不起作用?有人能解释一下吗?这对我也很有效。谢谢@William的回答@Vikas天哪,非常感谢你。我已经用web.config安装程序修复了4个小时,但它不起作用。非常感谢@Kadaj欢迎兄弟