Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.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 如何让http post将数据发送到后端。前端是angular 2,后端是.net 5_Asp.net_Angular_Http Post - Fatal编程技术网

Asp.net 如何让http post将数据发送到后端。前端是angular 2,后端是.net 5

Asp.net 如何让http post将数据发送到后端。前端是angular 2,后端是.net 5,asp.net,angular,http-post,Asp.net,Angular,Http Post,以下是我得到的错误: XMLHttpRequest无法加载“”。回应 飞行前请求未通过访问控制检查:否 “Access Control Allow Origin”标头出现在请求的服务器上 资源。因此,不允许访问源“//localhost:3000”。 响应的HTTP状态代码为400 这是我的控制器: [EnableCors(origins: "http://localhost:3000", headers: "Access-Control-Allow-Origin", methods: "GET

以下是我得到的错误:

XMLHttpRequest无法加载“”。回应 飞行前请求未通过访问控制检查:否 “Access Control Allow Origin”标头出现在请求的服务器上 资源。因此,不允许访问源“//localhost:3000”。 响应的HTTP状态代码为400

这是我的控制器:

[EnableCors(origins: "http://localhost:3000", headers: "Access-Control-Allow-Origin", methods: "GET, POST")]
public class HomeController : ApiController
{
    [HttpPost]
    public IHttpActionResult Post([FromBody] book inbook)
    {
        book fromangular = inbook;
        var body = this.Request.Content.ReadAsAsync<book>();
        return Ok(fromangular);
    }
}

任何帮助都将不胜感激。

您在WebApi的HttpConfiguration上启用了CORS吗?我相信您需要显式地启用它,控制器属性才能工作

var config = new HttpConfiguration();
config.EnableCors();
或者您可以在全球范围内启用它

var config = new HttpConfiguration();
config.EnableCors(new EnableCorsAttribute("http://localhost:3000", "*", "*"));
希望这有帮助

更新

根据,在声明属性时尝试启用所有方法,然后禁用不希望授予访问权限的特定操作

比如说

[EnableCors(origins: "http://localhost:3000", headers: "*", methods: "*")]
public class HomeController : ApiController
{
    [HttpPost]
    public IHttpActionResult Post([FromBody] book inbook)
    {
        book fromangular = inbook;
        var body = this.Request.Content.ReadAsAsync<book>();
        return Ok(fromangular);
    }
    [DisableCors]
    // some action
}
[启用CORS(源代码:http://localhost:3000,标题:“*”,方法:“*”]
公共类HomeController:ApiController
{
[HttpPost]
公共IHttpActionResult帖子([FromBody]书本中的书)
{
书本=书内;
var body=this.Request.Content.ReadAsAsync();
返回Ok(从角度);
}
[禁用CORS]
//一些行动
}

此外,不确定这是否有帮助,但在文档中,“方法:“get,post”是小写的,但这并不能解释get方法工作正常的原因。

您在WebApi的HttpConfiguration上启用了CORS吗?我相信您需要显式地启用它,控制器属性才能工作

var config = new HttpConfiguration();
config.EnableCors();
或者您可以在全球范围内启用它

var config = new HttpConfiguration();
config.EnableCors(new EnableCorsAttribute("http://localhost:3000", "*", "*"));
希望这有帮助

更新

根据,在声明属性时尝试启用所有方法,然后禁用不希望授予访问权限的特定操作

比如说

[EnableCors(origins: "http://localhost:3000", headers: "*", methods: "*")]
public class HomeController : ApiController
{
    [HttpPost]
    public IHttpActionResult Post([FromBody] book inbook)
    {
        book fromangular = inbook;
        var body = this.Request.Content.ReadAsAsync<book>();
        return Ok(fromangular);
    }
    [DisableCors]
    // some action
}
[启用CORS(源代码:http://localhost:3000,标题:“*”,方法:“*”]
公共类HomeController:ApiController
{
[HttpPost]
公共IHttpActionResult帖子([FromBody]书本中的书)
{
书本=书内;
var body=this.Request.Content.ReadAsAsync();
返回Ok(从角度);
}
[禁用CORS]
//一些行动
}

另外,不确定这是否有帮助,但在文档中,“方法:“get,post”是小写的,但这并不能解释get方法工作正常的原因。

是的,我允许这样做。奇怪的是,让方法工作,但张贴拒绝工作。是的,我允许。奇怪的是,让方法起作用,但帖子却不起作用。