当使用jQuery向PHP发送PUT AJAX请求时出现405错误:方法不允许。

3

我只是想用PHP + jQuery实现一个类似REST的应用程序。

在第一次尝试中,我开始遇到这个错误

PUT http://... 405 (不允许的方法)

我把这个放在我的PHP脚本的第一行

header("Access-Control-Allow-Orgin: *");
header("Access-Control-Allow-Methods: *");

这是我的jQuery代码:

$(function(){
    $.ajax({
        url: 'localhost/myscript.php',
        type: 'PUT',
        success: function(response) {
            //...
        }
    });
});

在配置方面,我没有做任何特殊的设置。 为什么我的Ajax请求会被拒绝,我该怎么解决?


尝试使用 get 而不是 put,看看是否有效。 - Hackerman
1
尝试使用 http://localhost/myscript.php 作为 url - Jamie Bicknell
可能是一些配置错误... 参见:http://serverfault.com/questions/148865/enable-put-method-on-apache-for-any-php-script - Hackerman
1
请添加 header("Access-Control-Allow-Headers: *");,另外,__Access-Control-Allow-Orgin__ 是一个拼写错误吗? - hjpotter92
1个回答

1

变更:

$(function(){
    $.ajax({
        url: 'localhost/myscript.php',
        type: 'PUT',
        success: function(response) {
            //...
        }
    });
});

收件人:

$(function(){
    $.ajax({
        url: 'http://localhost/myscript.php',
        type: 'PUT',
        success: function(response) {
            //...
        }
    });
});

而且:

Access-Control-Allow-Origin,不是 Access-Control-Allow-Orgin

header("Access-Control-Allow-Orgin: *");

收件人:

header("Access-Control-Allow-Origin: *");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接