简体   繁体   English

VueJs + ASP.Net Core Web API 发出 post 请求

[英]VueJs + ASP.Net Core Web API Make a post request

I have a VueJs Frontend where I want to make a post request to my ASP.NET Core Web API.我有一个 VueJs 前端,我想在其中向我的 ASP.NET Core Web API 发出发布请求。 This is what the code in the VueJs app looks like: VueJs 应用程序中的代码如下所示:

methods: {
    async addProject(project){

        const res = await fetch('https://localhost:44357/test/data/CreateProject', {
            method: 'POST',
            headers: {
                'Content-type': 'application/json'
            },
            //mode: 'no-cors',
            body: JSON.stringify(project)
        })

        const data = await res.json()

        this.projects = [...this.projects, data]
    },
},

The url could be missleading so I want to clarify that this url is mapped inside the Web API to a method inside the controller which should get a project object which has only a name attribute in it.该 url 可能具有误导性,所以我想澄清一下,这个 url 在 Web API 内部映射到控制器内部的方法,该方法应该获取一个只有 name 属性的项目对象。

Now for the ASP.NET Core Web API I have set up cors so that I can make calls to it:现在对于 ASP.NET Core Web API,我已经设置了 cors 以便我可以调用它:

services.AddCors();
...
app.UseCors(
    options => options.WithOrigins("http://localhost:8080").AllowAnyMethod()
);

Now for the route mapping I did it like this:现在对于路由映射,我是这样做的:

endpoints.MapControllerRoute(
    name: "test",
    pattern: "test/data/CreateProject",
    defaults: new { controller = "Models", action = "createproject" }
);

//the function:
[HttpPost]
public string CreateProject([FromBody] Project data)
{
    Debug.WriteLine(data.Name);
    return "Ok";
}

Now the error that I get is that I cannot make a call because of cors policy which is weird for me.现在我得到的错误是我无法打电话,因为 cors 政策对我来说很奇怪。 I thought I allowed the vuejs app to make calls.我以为我允许 vuejs 应用程序拨打电话。 I then installed the chrome plugin CORS unblock which didnt solve this basically but in the network tab inside the website inspector I can see that there is a Post Request with status code 204. I just want to confirm that the data arrives in my Web Api.然后我安装了 chrome 插件 CORS unblock,它基本上没有解决这个问题,但是在网站检查器内的网络选项卡中,我可以看到有一个状态代码为 204 的 Post Request。我只想确认数据是否到达我的 Web Api。 What am I doing wrong and what do I need to change in order to make this happen?我做错了什么,我需要改变什么才能做到这一点?

app.UseCors(
    options => options.WithOrigins("http://localhost:8080").AllowAnyMethod().AllowAnyHeader()
);

Using AllowAnyHeader function fixed it使用 AllowAnyHeader 功能修复它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Asp.net core 3 Web Api post 请求不起作用 - Asp.net core 3 Web Api post request not working 如何从 ASP.NET 核心 mvc 向 ASP.NET 核心中的 Web API 发出 PUT 请求? - How to make a PUT request from ASP.NET core mvc to Web API in asp.net core? 将文件从 ASP.NET Core web api 发布到另一个 ASP.NET Core web api - Post files from ASP.NET Core web api to another ASP.NET Core web api 如何将 POST 请求正文中的有效 XML 提交到使用 XML 的 ASP.NET Core Web API? - How to submit valid XML in a POST request body to an ASP.NET Core Web API that consumes XML? 从ASP.Net核心Web API中的发布请求在基础控制器中分配值 - Assign Value In Base Controller From Post Request In ASP.Net core web API 使用ASP.NET Core MVC控制器将RestSharp POST请求连接到Web API - issue connecting RestSharp POST request to web API with ASP.NET Core MVC controller Web API ASP.NET 核心发布请求 415 不支持的媒体类型 - Web API ASP.NET Core Post Request 415 Unsupported Media Type 使用 ASP.NET Core MVC 将带有文件的数据发布到 api (ASP.NET Core Web API) - Post data with files using ASP.NET Core MVC to api (ASP.NET Core Web API) 如何启用 CORS 并使其在 Asp.net 核心 Web ZDB97423871438ACF19393F20C1EC91B750799EC5EEBZ 中正常工作 - How to enable CORS and make it work properly with POST in Asp.net Core Web API? ASP.net 核心 Web API post 数组作为属性 - ASP.net core Web API post array as a property
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM