POST 405 不允许使用特定的 XMLHttpRequest 和 PHP

Posted

技术标签:

【中文标题】POST 405 不允许使用特定的 XMLHttpRequest 和 PHP【英文标题】:POST 405 Not Allowed with Particular XMLHttpRequest and PHP 【发布时间】:2012-11-19 05:38:00 【问题描述】:

我收到一个 POST https://www.example.com/php/download.php 405(不允许),这个 ajax 调用我的 php 脚本。它用于我编写的用于下载文件的 http 文件服务器...使用 php 在 Web 根目录之外获取它们。我的其他 PHP 脚本/ajax 调用适用于我的 http 文件服务器......我只是在这个特定的 ajax 调用/php 脚本中遇到了这个问题。我正在使用 nginx

再次强调一下……在同一台服务器上,其他 ajax 调用和其他 php 脚本也可以工作。所以,有点输了。

<?php

$path = "/trunk/";
$file = $_POST['filename'];
$file_ext = pathinfo($file, PATHINFO_EXTENSION);
$file_name = pathinfo($file, PATHINFO_BASENAME);
$file_full = $path . $file_name;

$finfo = new finfo(FILEINFO_MIME);

$ctype = $finfo -> file($file_full);
header('Content-Disposition: attachment; filename=\"$file_name\"');
header("Content-Type: " .$ctype);

readfile($file_full);

?>

187 
188 function downloadFile(event) 
189         var fname = event.target.getAttribute("href");
190         fname.replace("#","");
191         var form = new FormData();
192         form.append("filename", fname);
193         
194         var xhr = new XMLHttpRequest();
195         xhr.onreadystatechange = function () 
196 //              setTimeout(refresh, 500);
197         
198         
199         xhr.open("POST", "php/download.php", true);
200         xhr.send(form);
201 

这是来自 Google 的 chrome 网络调试器:

Request URL:https://www.example.com/php/download.php
Request Method:POST
Status Code:405 Not Allowed
Request Headersview source
Accept:*/*
Accept-Charset:ISO-8859-1,utf-8;q=0.7,*;q=0.3
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:172
Content-Type:multipart/form-data; boundary=----WebKitFormBoundaryNGHvOaUnlBV8ADWE
Host:www.example.com
Origin:https://www.example.com
Referer:https://www.example.com/index.php
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.4 (Khtml, like Gecko) Chrome/22.0.1229.79 Safari/537.4
Request Payload
------WebKitFormBoundaryNGHvOaUnlBV8ADWE
Content-Disposition: form-data; name="filename"

CentOS-6.3-x86_64-minimal-EFI.iso
------WebKitFormBoundaryNGHvOaUnlBV8ADWE--
Response Headersview source
Connection:keep-alive
Content-Length:568
Content-Type:text/html
Date:Fri, 30 Nov 2012 23:59:59 GMT
Keep-Alive:timeout=300
Server:nginx

【问题讨论】:

【参考方案1】:

你的 nginx 配置,检查一下。您的位置块不允许您访问该文件。

【讨论】:

【参考方案2】:

我忘记了在 nginx.conf 中我在 location 块中单独指定了每个 php 文件。我忘了将 download.php 文件添加到其中。感谢 Jacob 和 Guilherme 提供的帮助!

【讨论】:

【参考方案3】:

修复:

header('Content-Disposition: attachment; filename="'.$file_name.'"');

修复:

     xhr.onreadystatechange = function () 
        if (r.readyState===4 && r.status===200) 
           //setTimeout(refresh, 500);
        
     

[编辑]

试试:

xhr.open("POST", "php/download.php", true);
xhr.onreadystatechange = function () 
    ...

xhr.send(form);

【讨论】:

没有成功。我在 PHP 脚本中放置了 error_log("foo") 并且脚本甚至没有运行......因为日志中没有任何内容(确保日志记录有效)。我确实确保在调试器中正在运行 ajax 调用。 php/download.php 路径对吗?检查这个罐子会去别的地方。我编辑了我的答案,试试看。 没有测试正确,使用GoogleChrome(或safari)和inspect element,转到名为Network的选项卡,然后执行ajax。获取Request Headers 并添加您的问题。 是的,路径是正确的。我在这个文件服务器和相同的路径中有其他 php 脚本。但是脚本甚至没有被调用。问题是 POST 不允许某些东西(不知道是什么)。我查看了调试器,fname 是“file_name.bar”,所以 POST 应该可以处理表单数据(无论如何我都会这么认为)。相当坚持为什么 POST 抱怨。 GoogleChrome(或safari)与inspect element一起使用,转到名为Network的选项卡,然后执行ajax。获取Request Headers 并添加您的问题。

以上是关于POST 405 不允许使用特定的 XMLHttpRequest 和 PHP的主要内容,如果未能解决你的问题,请参考以下文章

使用 requests.post 登录导致“错误 405 不允许”

Angular 7:发送 post 请求给出错误 405(不允许的方法)

Axios POST 方法:405 方法不允许

jQuery .ajax() POST 请求抛出 405(不允许的方法)但 GET 不会

“405 方法不允许” - 使用 ASP.NET jQuery ajax POST 时

方法不允许 (POST) Django 405 错误