Wednesday, 13 January 2021

HTTP 405 error when ajax function calls php script

I am trying to call the php script with the help of AJAX and send some values acquired from an html form.

My code:

function submit(a,b,c,d,em){

    console.log(a+" "+b+" "+c+" "+d+" "+em)   //shows the values correctly

    $.ajax({
        type : "POST",  //type of method
        url  : "script.php",  //your page
        data : { a:a, b:b, c:c, d:d, e:em },// passing the values
        success: function(){  
                                //do what you want here...
                                alert("ok");
                }
    });

    
}

const dForm = document.getElementById('form');          
dForm.addEventListener('submit', function(e) {
    e.preventDefault()
    submit(a,b,c,d,em);
});

When I run the script, I get this error in the console:

Failed to load resource: the server responded with a status of 405 (Method Not Allowed)

Please help me in fixing this.

Here's my php script:

<?php
    header('Access-Control-Allow-Origin: *');
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

    if (isset($_POST["a"]))
        $val_1=$_POST['a'];
    if (isset($_POST["b"]))
        $val_2=$_POST['b'];
    if (isset($_POST["c"]))
        $val_3=$_POST['c'];
    if (isset($_POST["d"]))
        $val_4=$_POST['d'];
    if (isset($_POST["e"]))
        $val_5=$_POST['e'];
?>      


from HTTP 405 error when ajax function calls php script

No comments:

Post a Comment