From 6704b812adf90a4ec8ba4834d712f6703063d215 Mon Sep 17 00:00:00 2001 From: Braga Lund Date: Sun, 28 Nov 2021 11:51:37 +0100 Subject: [PATCH] Made frontend and backend talk to eachother --- backend/README.md | 5 +++ backend/bragapi/Program.cs | 12 ++++++ backend/bragapi/bragapi.csproj | 2 +- frontend/braggalot/src/App.vue | 5 ++- .../src/components/WeatherForecast.vue | 37 +++++++++++++++++++ frontend/braggalot/src/main.js | 4 ++ .../src/services/api/WeatherForecastApi.js | 14 +++++++ 7 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 backend/README.md create mode 100644 frontend/braggalot/src/components/WeatherForecast.vue create mode 100644 frontend/braggalot/src/services/api/WeatherForecastApi.js diff --git a/backend/README.md b/backend/README.md new file mode 100644 index 0000000..0fcb96c --- /dev/null +++ b/backend/README.md @@ -0,0 +1,5 @@ +# Bragapi + +## TODO + +* [x] fix cors preflight request locally https://stackoverflow.com/questions/20792950/net-web-api-cors-preflight-request \ No newline at end of file diff --git a/backend/bragapi/Program.cs b/backend/bragapi/Program.cs index 6f0b85f..99a46da 100644 --- a/backend/bragapi/Program.cs +++ b/backend/bragapi/Program.cs @@ -11,6 +11,16 @@ builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(); +builder.Services.AddCors(options => +{ + options.AddPolicy("CorsPolicy", + builder => builder.AllowAnyOrigin() + .AllowAnyMethod() + .AllowAnyHeader() + .AllowCredentials() + .WithOrigins("http://localhost:8080")); +}); + var app = builder.Build(); // Configure the HTTP request pipeline. @@ -20,6 +30,8 @@ app.UseSwaggerUI(); } +app.UseCors("CorsPolicy"); + app.UseHttpsRedirection(); app.UseAuthorization(); diff --git a/backend/bragapi/bragapi.csproj b/backend/bragapi/bragapi.csproj index 05df6a3..5512e09 100644 --- a/backend/bragapi/bragapi.csproj +++ b/backend/bragapi/bragapi.csproj @@ -7,7 +7,7 @@ - + diff --git a/frontend/braggalot/src/App.vue b/frontend/braggalot/src/App.vue index 620121a..4b9a79f 100644 --- a/frontend/braggalot/src/App.vue +++ b/frontend/braggalot/src/App.vue @@ -1,15 +1,18 @@ + + \ No newline at end of file diff --git a/frontend/braggalot/src/main.js b/frontend/braggalot/src/main.js index 01433bc..69ed912 100644 --- a/frontend/braggalot/src/main.js +++ b/frontend/braggalot/src/main.js @@ -1,4 +1,8 @@ import { createApp } from 'vue' import App from './App.vue' +import axios from 'axios' + +axios.defaults.baseURL = 'https://localhost:7189' +axios.defaults.headers.common['Access-Control-Allow-Origin'] = '*'; createApp(App).mount('#app') diff --git a/frontend/braggalot/src/services/api/WeatherForecastApi.js b/frontend/braggalot/src/services/api/WeatherForecastApi.js new file mode 100644 index 0000000..ea214f2 --- /dev/null +++ b/frontend/braggalot/src/services/api/WeatherForecastApi.js @@ -0,0 +1,14 @@ +import axios from 'axios' + +export default { + getWeatherForcasts(){ + return axios.get('/weatherforecast',{ + headers: { + + } + }) + .then(response => { + return response.data + }) + }, +} \ No newline at end of file