Click here to Skip to main content
15,121,818 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to call the php script with the help of AJAX and send some values acquired from an html form. My code editor is Microsoft Visual Studio Code.

My code:

JavaScript
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:

JavaScript
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
<?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'];
    ?>




What I have tried:

Included both the core headers, but still it doesn't work (in my php scirpt).
Posted
Updated 13-Jan-21 5:02am
v4
Comments
Richard Deeming 13-Jan-21 4:51am
   
The line that throws the error doesn't appear in either of your code blocks, and doesn't appear to be part of jQuery's ajax method. Is there some relevant code missing from your question?
Member 14079444 13-Jan-21 5:04am
   
Yes, sorry I forgot to include it before. The error is thrown by the above mentioned line in the jquery file mentioned in the script tag

1 solution

   
Comments
Member 14079444 13-Jan-21 1:32am
   
Can you tell the solution to this problem w.r.t. the text editor Visual Studio code? I mean do I have to change any configuration file. I couldn't understand any of the contents given above.
thatraja 13-Jan-21 1:37am
   
Unfortunately you need to spend some time on articles & threads to fix your issues. For example, 1st thread suggesting to change POST into GET to get it worked. Try that first. Always execute minimum code to ensure it works.

You'll get more solutions if you search Internet.
Member 14079444 13-Jan-21 1:43am
   
I've tried using 'GET', it shows this error then, ERR_CONNECTION_RESET. Plus, I don't want to use GET, as my script has some sensitive information.

I've also tried with $_REQUEST, $_COOKIE. They also show the same error. Nothing works
thatraja 13-Jan-21 1:53am
   
Other threads have multiple tricks to fix the issue. And if you search web with same error message, you'll get more answers. This particular issue is not related to your code. It's related to IIS, Server, etc., things. So check other threads.
Sometime you'll get answers within minutes, sometime it'll take day or more. It depends.
Member 14079444 13-Jan-21 1:57am
   
I am working on Visual Studio Code editor, and I've searched in the internet, but I've found no solution regarding this editor (it's either Lavarel or Eclipse, etc). As far as I know, I need to change something in the web config file, but where is it?
thatraja 13-Jan-21 2:16am
   
Check this
https://www.php.net/manual/en/configuration.file.php

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900