简体   繁体   English

带有 ajax jQuery 的 405(不允许的方法)(POST)方法

[英]405 (Method Not Allowed) (POST) method with ajax jQuery

im trying to send some data through ajax jQuery to a php file with POST but i keep getting POST 405 Method Not Allowed error, any ideas to solve this issue will be appreciated, heres the function that makes the call我试图通过 ajax jQuery 将一些数据发送到带有 POST 的 php 文件,但我不断收到 POST 405 Method Not Allowed 错误,任何解决此问题的想法将不胜感激,这是进行调用的函数

function manageData(key) {
    var name = $("#countryName");
    var abbrev = $("#countryAbbrev");
    var desc = $("#countryDesc");

    if (isNotEmpty(name) && isNotEmpty(abbrev) && isNotEmpty(desc)) {
        $.ajax({
            url: 'http://127.0.0.1:5500/ajax.php',
            method: 'POST',
            dataType: 'text',
            data: {
                key: key,
                name: name.val(),
                abbrev: abbrev.val(),
                desc: desc.val()
            },
            success: function (response) {
                alert(response);
            }
        });
    }
}

and here is the ajax.php file code这是 ajax.php 文件代码

<?php
    if (isset($_POST['key'])) {

        $conn = new mysqli(host:'localhost', username:'root', passwd:'root', 
        dbname:'mysqldatamanager');
        $name = $conn->real_escape_string($_POST['name']);
        $abbrev = $conn->real_escape_string($_POST['abbrev']);
        $desc = $conn->real_escape_string($_POST['desc']);

        if($_POST['key'] == 'addNew') {
            $sql = $conn->query(query:"SELECT id FROM country WHERE 
            countryName = '$name'");
            if ($sql->num_rows > 0) {
                exit("Country already exists!");
            } else {
                $conn->query("INSERT INTO country (countryName,                     
                countryAbbrev, countryDesc) VALUES ('$name', '$abbrev', 
                '$desc')");
                exit("Country has been added succesfully!");
            }   
        }
    }
?>

Please try below code.请尝试以下代码。

function manageData(key) {
    var name = $("#countryName");
    var abbrev = $("#countryAbbrev");
    var desc = $("#countryDesc");

    if (isNotEmpty(name) && isNotEmpty(abbrev) && isNotEmpty(desc)) {
        $.ajax({
            url: 'http://localhost/ajax.php',
            type: "POST",
            data: {
                key: key,
                name: name.val(),
                abbrev: abbrev.val(),
                desc: desc.val()
            },
            success: function (response) {
                alert(response);
            }
        });
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM