不允许使用AJAX POST方法错误405

时间:2017-07-05 07:17:45

标签: php ajax

我点击按钮时尝试发送电子邮件。为此,我对一个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方法。有谁知道我做错了什么?

3 个答案:

答案 0 :(得分:0)

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

您的代码应该如下......

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);
    });

});

答案 1 :(得分:0)

试试这个

 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);
   }
  });
 }

答案 2 :(得分:0)

恕我直言,如果你不允许使用405 post方法,那么解决方法(好吧,不是真正的解决方案)可以尝试使用这些设置:

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);
  });
 }