1

I want to send the information entered in my form to my database when the submit button is clicked

<button onclick="submitIntervention()" class="btn btn-primary" type="submit">Submit</button>

to do so I make a ajax call inside my submitIntevention() in my view like this.

 $.ajax({
    url: 'https://API-CALL-GOES-HERE',
    type: 'POST',
    data: JSON.stringify(interventionData),
    headers: {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Methods": "HEAD, GET, POST, PUT, PATCH, DELETE",
        "Access-Control-Allow-Headers": "Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With",
        "Accept": "application/json",
        "Content-Type": "application/json"
    },

At first I was having problems with Cors so I went on Azure and allowed everything to go through with the '*' and added this code to my API startup.cs file.

services.AddCors(policy => policy.AddPolicy("Policy", builder =>
        {
            builder.AllowAnyOrigin().AllowAnyMethod().AllowAnyHeader();
        }));


app.UseCors("Policy");

Since then I stopped getting the Cors error but I get the internal 500 one. I read about checking my logs to find the mistake but not quite sure how to go about this since I'm a beginner. thanks for your time.

EDIT (azure Cors configuration of my api) enter image description here

EDIT 2

When I comment out the "Content-Type" line I get the 415 error for the unsupported media type which makes me think there's a problem there

  • CORS are server side headers, not request headers. it might cause a 500 but you'd have to check your server logs. – Daniel A. White Dec 18 '21 at 19:43
  • Like I mentioned in your last question (that I believe I'm still waiting for a response from you for) cors is a server side thing; it's not something you add as headers in the JavaScript .. I don't think the cors code you've added to the c# is doing anything because azure is handling it for you. However if you make a request in script that requires cors and the server error 500's on the response to the OPTIONS then the cors check will usually fail – Caius Jard Dec 18 '21 at 20:56
  • Let's try and stick to one question to work through this error on – Caius Jard Dec 18 '21 at 20:57
  • I just saw it sorry about that I cant comment on it because I dont have enough reputation. what do you mean by show your configuration the API one or the app one. thanks a lot @CaiusJard – user17710829 Dec 18 '21 at 21:14
  • What you have set up in azure portal - add a screenshot to your question – Caius Jard Dec 18 '21 at 21:46
  • done, I did the the same for my app portal too. when I did this the Cors error went away and since then I've been struggling with the 500 error @CaiusJard – user17710829 Dec 18 '21 at 21:58
  • 500 is probably an unhandled exception occurring. Run the site on your own dev environment and make a request to it, or enable developer exception page on azure and set the environment variable in azure to make the site think it's is running in dev mode. https://stackoverflow.com/questions/31992141/show-asp-net-5-error-page-in-azure-web-app - and make a request to the api using a tool like Insomnia, not a browser – Caius Jard Dec 19 '21 at 07:02
  • Try to call your api with tools like postman first to make sure if your api has some issue. If it's ok to call the api directly, maybe you can try to comment the ` headers: {},` in your ajax. 500 error is easy to debug – Tiny Wang Dec 19 '21 at 08:35
  • Thanks for the replies I tried calling my api with postman but I get a 415 unsupported media error, I tried selecting the raw/json option in postman but same result, when I comment out the "content-type" line it doesnt give the 500 error but the 415 one in the console which makes me think there might be a problem there. – user17710829 Dec 19 '21 at 14:41
  • shame on me I had a typo in my api... thanks alot for helping me – user17710829 Dec 19 '21 at 16:39
  • After the call is made, in the Browser, Inspect the console (right button click on the page, then click on Inspect). If the call is being rejected due to cross domain issues, there will be an error shown in the console. – Allen King Dec 19 '21 at 16:43
  • @user17710829 Can you please specify what your typo was? I'm having this exact same problem as you. – malthe.w Mar 15 '23 at 12:36

0 Answers0