【问题标题】:AJAX POST-method not allowed Error 405不允许 AJAX POST 方法错误 405
【发布时间】:2017-07-05 07:17:45
【问题描述】:

单击按钮时,我正在尝试发送电子邮件。为此,我对 php 文件进行了 ajax 调用:

Ajax 调用:

 function postAjax(arr) {
     $.ajax({
         url: "../BackEnd/AjaxHandler.php",
         data: { 'action': 'mail' },
         type: "GET"
     });
 }

php 文件 (Ajaxhandler.php)

<?php   
   if(isset($_GET['action']) && !empty($_GET['action'])) {
   $action = $_GET['action'];
   switch($action){
      case 'mail':
         mail('N.Suhner@wigasoft.ch', 'Mein Betreff', 'tsest'); 
         break;
      }
   }
?>

呼叫:

$("#mailto").click(function() {
   postAjax(listOfTasks);
 });

在通话中我做了更多的事情,但这些不相关(样式)。

当我单击按钮时,我收到一个错误,即不允许使用 POST 方法。有谁知道我做错了什么?

【问题讨论】:

  • 尝试在您的 ajax 属性中添加 dataType "JSONP"
  • 请从浏览器控制台复制粘贴错误
  • 请发布您的路线文件。
  • 也许您的 405 post 方法不允许?尝试使用类型:“get”和数据类型:“jsonp”。

标签: php ajax


【解决方案1】:

有一个错字,您输入了错误的关键字...请检查以下代码

您的代码应如下所示......

function postAjax(arr) {
   $.ajax({
      type: "POST",
      url: "../_BackEnd/AjaxHandler.php",
      crossDomain: true,
      data: { id: 12 }
   });
}

php 文件:

<?php        
     $message = "This is a text!";
     mail('example@mail.ch', 'subject', $message);       
?>

编辑

HTML

<form id="foo">
    <label for="bar">A bar</label>
    <input id="bar" name="bar" type="text" value="" />

    <input type="submit" value="Send" />
</form>

jQuery

// Variable to hold request
var request;

// Bind to the submit event of our form
$("#foo").submit(function(event){

    // Prevent default posting of form - put here to work in case of errors
    event.preventDefault();

    // Abort any pending request
    if (request) {
        request.abort();
    }
    // setup some local variables
    var $form = $(this);

    // Let's select and cache all the fields
    var $inputs = $form.find("input, select, button, textarea");

    // Serialize the data in the form
    var serializedData = $form.serialize();

    // Let's disable the inputs for the duration of the Ajax request.
    // Note: we disable elements AFTER the form data has been serialized.
    // Disabled form elements will not be serialized.
    $inputs.prop("disabled", true);

    // Fire off the request to /form.php
    request = $.ajax({
        url: "/form.php",
        type: "post",
        data: serializedData
    });

    // Callback handler that will be called on success
    request.done(function (response, textStatus, jqXHR){
        // Log a message to the console
        console.log("Hooray, it worked!");
    });

    // Callback handler that will be called on failure
    request.fail(function (jqXHR, textStatus, errorThrown){
        // Log the error to the console
        console.error(
            "The following error occurred: "+
            textStatus, errorThrown
        );
    });

    // Callback handler that will be called regardless
    // if the request failed or succeeded
    request.always(function () {
        // Reenable the inputs
        $inputs.prop("disabled", false);
    });

});

【讨论】:

  • 我改变了它已经看到@Exprator的答案,但我仍然得到同样的错误。
  • @NiZelooer 让我给你测试代码大致相同,请检查编辑部分
  • 我像你一样更改了 jquery,在控制台中我收到了不允许该方法的错误。
【解决方案2】:

试试这个

 function postAjax(arr) {
  $.ajax({
   type: "POST",
   url: "../_BackEnd/AjaxHandler.php",
   data: { id: 12 },
   contentType: "application/json; charset=utf-8",
   dataType: "json",
   success: function (data) {
       alert(data);
   }
  });
 }

【讨论】:

  • 很遗憾没有帮助。
【解决方案3】:

恕我直言,如果您不允许使用 405 发布方法,可以尝试使用以下设置解决方法(好吧,不是真正的解决方案):

function postAjax(arr) {
  $.ajax({
   type: "GET",
   url: "../_BackEnd/AjaxHandler.php?id=12",
   dataType: "json"
  }).done(function(d){
      console.log(d);
  }).fail(function(j){
      console.log(j.responseText);
  });
 }

【讨论】:

  • 我像您的答案一样更改了它,现在我收到了 404.3 - Not found 错误。
  • 因为配置扩展,页面无法访问。当它是一个脚本时,我需要添加一个处理程序。当它应该被下载时,我需要添加一个 MIME 分配
  • 您应该尝试了解代码在 AjaxHandler.php 中的作用。您应该发布更多代码以了解 AjaxHandler.php 中的内容。您可以访问此代码吗?
  • 在 Ajaxhandler.php 中仅此而已 ^^.
  • 如果你调试脚本注释 mail() 函数并尝试将某些内容回显到 js 怎么办?也许可以链接在 mail() ini 配置参数或标题中?如果不传递 _post 或 _get 数据怎么办?一定有什么东西“扰乱”了剧本。也许我们的服务器配置未知?我将尝试最简单的 ajax 调用,然后添加更多功能。
猜你喜欢
  • 2019-05-31
  • 2018-03-07
  • 1970-01-01
  • 2019-07-05
  • 2013-12-05
  • 2012-10-26
  • 1970-01-01
  • 2017-10-16
  • 1970-01-01
相关资源
最近更新 更多