Home > OS >  Can not post request in HTML JS | No 'Access-Control-Allow-Origin' header is present on th
Can not post request in HTML JS | No 'Access-Control-Allow-Origin' header is present on th

Time:09-11

I'm making a Java Compiler website for myself and I'm using JDoodle
In the document of this API, shows NodeJS code execute function (https://docs.jdoodle.com/integrating-compiler-ide-to-your-application/compiler-api)
and I'm using javascript in html so I change a little bit to

// using jQuery
$.ajax({
        url,
        type: 'post',
        data,
        headers: {
            'Content-Type': 'application/json'
        },
        dataType: 'json',
        success: function(data) {
            console.log(data);
        }
    })

but it gave me this error

Access to XMLHttpRequest at 'https://stage.jdoodle.com/execute' from origin has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

I also tried $.post and Why does my JavaScript code receive a "No 'Access-Control-Allow-Origin' header is present on the requested resource" error, while Postman does not? but still the same.
Thank you!!

CodePudding user response:

You need to enable header on the server side.

This has been asked before Enable CORS in fetch api

Alternatively you can ignore CORS with a flag.

CodePudding user response:

Add csrf_token in your header of layout

<meta name="csrf-token" content="{{ csrf_token() }}">

and then , change you header in ajax

headers: {
   'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
  • Related