[英]Angular http post request - No 'Access-Control-Allow-Origin' header is present on the requested resource
[英]Can not post request in HTML JS | No 'Access-Control-Allow-Origin' header is present on the requested resource
我正在为自己制作一个 Java 编译器网站,我正在使用 JDoodle
在 API 的文档中,显示 NodeJS 代码执行 function ( https://docs.jdoodle.com/integrating-compiler-ide-to-compiler
我在 html 中使用 javascript 所以我稍微改变一下
// using jQuery
$.ajax({
url,
type: 'post',
data,
headers: {
'Content-Type': 'application/json'
},
dataType: 'json',
success: function(data) {
console.log(data);
}
})
但它给了我这个错误
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.
我还尝试$.post
, 为什么我的 JavaScript 代码会收到“没有 'Access-Control-Allow-Origin' header 出现在请求的资源上”错误,而 Z6F061E1CAD5E2783CEA0B2976ED2F 没有? 但还是一样。
谢谢!!
在布局的 header 中添加csrf_token
<meta name="csrf-token" content="{{ csrf_token() }}">
然后,将您的 header 更改为 ajax
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
},
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.