【问题标题】:Ajax 405 (Method Not Allowed) Cross Domain IssueAjax 405(不允许的方法)跨域问题
【发布时间】:2023-03-05 18:33:01
【问题描述】:

我正在尝试从 localhost 运行此脚本,但收到错误 405 (Method Not Allowed)

代码

$(document).ready(function(){

    $.ajax({
        url:'http://some.url/',
        type:'post',
        //crossDomain: true,
        data:'{id:"49"}',
        contentType: "application/json; charset=utf-8",
        dataType:'jsonp',

            success: function(responseData, textStatus, jqXHR) {
                alert('right');
                var value = responseData.someKey;
                console.log(value);
            },
            error:function() {
                alert("Sorry, I can't get the feed");  
            }
    });
});

我尝试了crossDomain:true$.support.cors = true,但没有用。有什么想法吗?

【问题讨论】:

  • 实际服务器是否返回正确的 CORS 标头?设置这些属性只会使其检查这些标题。
  • @Qantas94Heavy 我无法访问服务器代码。如何检查?
  • @Rohan Kumar 谢谢。现在更新了网址。我之前放了一个虚拟网址。

标签: javascript jquery ajax cross-domain


【解决方案1】:

这与 CORS 无关,HTTP 405 表示您的 HTTP 方法不被允许,例如GET、POST 等。

根据 Chrome 开发工具,HTTP 方法只允许使用 POST 和 OPTIONS。

要使用jQuery.ajax() 发送 POST 请求,请使用以下命令:

$.ajax('url', {
    // other settings here
    type: 'POST'
}

您也可以使用jQuery.post() 包装方法来做同样的事情。

请注意,有问题的特定站点尚未设置跨域支持,因此如果您需要访问此站点,您需要让该站点解决此问题,否则您需要使用您的服务器作为代理。

【讨论】:

  • 感谢回复。什么是反向代理请求??
猜你喜欢
  • 2014-06-22
  • 2017-05-31
  • 1970-01-01
  • 2020-07-04
  • 1970-01-01
  • 1970-01-01
  • 2012-06-28
  • 2019-05-10
  • 1970-01-01
相关资源
最近更新 更多