在标题(php)中启用CORS不能在wordpress网站上运行[重复]
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在标题(php)中启用CORS不能在wordpress网站上运行[重复]相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我正在尝试使用jquery发送请求并从其他网站获得响应。发送请求,我有200.但是我无法从服务器收到响应消息,我看到evretime是错误:(我使用wordpress与woocomerce。我的服务器在bluehost上)
Failed to load https://smm.nakrutka.by/api/?key=76856&action=create&service=34&quantity=100&link=https://www.instagram.com/p/BnWW4npDAU5/: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://fejmu.pl' is therefore not allowed access.
scripts.js:259 Cross-Origin Read Blocking (CORB) blocked cross-origin response https://smm.nakrutka.by/api/?key=3262a5221b2643d925fcd5752d021b2f&action=create&service=34&quantity=100&link=https://www.instagram.com/p/BnWW4npDAU5/ with MIME type application/json. See https://www.chromestatus.com/feature/5629709824032768 for more details.
(anonymous) @ scripts.js:259
sentOrderAPIRequest @ scripts.js:243
Promise.then (async)
(anonymous) @ scripts.js:40
dispatch @ jquery-1.11.2.min.js:3
r.handle @ jquery-1.11.2.min.js:3
这是我的标题:
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
我的请求API函数
function sentOrderAPIRequest(validation){
return new Promise(function(resolve) {
if (validation) {
var $number = $('#order_quantity');
var numberValue = Number($number.val());
var $link = $('#instagram_link');
var linkvalue = $link.val();
var serviceIdValue = $('#new-order-select2').val();
var url = "https://smm.nakrutka.by/api/?key=564564&action=create&service="+ serviceIdValue +"&quantity=" + numberValue + "&link=" + linkvalue + "";
var request = new XMLHttpRequest();
request.open('GET', url);
request.onreadystatechange = function() {if (request.readyState==4) alert("It worked!");};
request.setRequestHeader("Content-type", "application/json");
request.send();
# BEGIN WordPress
<IfModule mod_rewrite.c>
Header always set Access-Control-Allow-Origin "*"
Header always set Access-Control-Allow-Headers "x-requested-with, Content-Type, origin, authorization, accept, client-security-token"
Header always set Access-Control-Allow-Methods "GET,PUT,POST,DELETE,OPTIONS"
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
END WordPress
答案
你不需要CORS。
您可以在自己的站点上调用代理脚本,然后使用cURL或Guzzle从PHP进行调用。这是我一直这样做的方式。
jQuery的:
$.get('/your/proxy/url.php', {any: "vars", go: "here"}, function(data){
console.log(data);
});
PHP:
<?php
$data = file_get_contents('the url you are calling from js here');
header('Content-Type: application/javascript'); // if $data is JSON
echo $data;
exit;
以上是关于在标题(php)中启用CORS不能在wordpress网站上运行[重复]的主要内容,如果未能解决你的问题,请参考以下文章