邮递员和AJAX标头不一致的PHP标头()

时间:2018-10-04 23:46:07

标签: php ajax cors postman litespeed

这是对此的继续。 I am getting No 'Access-Control-Allow-Origin' after countless times of my script working 但是为此,我找到了一个快速修复程序,您可以看到我在谈论htaccess,事实证明我的PHP问题是litespeed的行为方式。

问题是快速修复很不错,但我仍然希望自己有一个可以动态更改标头的PHP代码。

这是我的php代码

<?php ob_start();
header("Access-Control-Allow-Origin:{$_SERVER['HTTP_ORIGIN']}");
header("Access-Control-Expose-Headers:Access-Control-Allow-Origin");
header('Access-Control-Allow-Credentials:true');
header("Cache-Control:no-cache");
header("Pragma:no-cache");
header("Access-Control-Allow-Methods:POST,GET,DELETE,OPTIONS");
header('Allow', 'GET,POST,DELETE,OPTIONS');
echo "{'hi':'bye'}";
ob_end_flush();?>

所以,是的,当我使用邮递员对其进行测试时,这种方法效果很好,它可以在POST和AJAX上运行,但不能在DELETE和OPTIONS上工作。

我还是

Failed to load http://api.website.com/abc.php: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://website.com' is therefore not allowed access

作为错误。

当我在Postman上获得所需的内容时,我发现标题在AJAX上发生了变化。为什么?

邮递员:

Access-Control-Allow-Credentials →true
Access-Control-Allow-Methods →POST,GET,DELETE,OPTIONS
Access-Control-Allow-Origin →
Access-Control-Expose-Headers →Access-Control-Allow-Origin
CF-RAY →464b5312c556591b-DFW
Cache-Control →no-cache
Connection →keep-alive
Content-Encoding →gzip
Content-Type →text/html; charset=UTF-8
Date →Thu, 04 Oct 2018 23:10:53 GMT
Pragma →no-cache
Server →cloudflare
Transfer-Encoding →chunked
Vary →Accept-Encoding
X-Turbo-Charged-By →LiteSpeed

AJAX:

$.ajax({
    url: 'http://api.cubclub.tk/abc.php',
    type: 'DELETE',
    success:function(r) {
        console.log(r)
    },
    error:function(r) {
        // window.location = '404';
    }
});

Request URL: http://api.website.tk/abc.php
Request Method: OPTIONS
Status Code: 200 OK
Remote Address: 104.27.183.4:80
Referrer Policy: no-referrer-when-downgrade
Allow: OPTIONS,HEAD,GET,POST
CF-RAY: 464b7e274229589d-DFW
Connection: keep-alive
Content-Length: 0
Content-Type: text/plain
Date: Thu, 04 Oct 2018 23:40:18 GMT
Server: cloudflare
Set-Cookie: __cfduid=df8bca79dfe244fd91f4dbe92487db7681538696418; expires=Fri, 04-Oct-19 23:40:18 GMT; path=/; domain=.cubclub.tk; HttpOnly
X-Turbo-Charged-By: LiteSpeed
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Access-Control-Request-Method: DELETE
Connection: keep-alive
Host: api.website.tk
Origin: http://website.tk
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36

我希望我不会冒犯我如何继续发展成为其他问题的问题,作为一名作家,我对于stackoverflow还是相当陌生的。

0 个答案:

没有答案