Search code examples
phpjqueryjsonajaxjson-server

error 404 on post request with ajax, php and json server


As said in the title, i get this error

jquery.min.js:3049 POST http://localhost:3000/php/add_events.php 404 (Not Found)

when i try to access to a php file with that code on a json-server :

$.ajax({
    url: './php/add_events.php',
    data: 'title='+ eventData.title+'&start='+ eventData.start +'&end='+ eventData.end +'&areHere='+eventData.areHere+'&finalConsult='+eventData.finalConsult ,
    type: "POST",
    success: function(json) {
        alert("OK");
        eventData.id = json
    }
});

and when i try to set GET instead of POST, it works but it doesn't post (normal).
Here's the php code :

<?php

$title=$_POST['title'];
$start=$_POST['start'];
$end=$_POST['end'];
$areHere=$_POST['areHere'];
$finalConsult=$_POST['finalConsult'];
$typeConsult=$_POST['typeConsult'];
try {
 $bdd = new PDO('mysql:host=localhost;dbname=agenda', 'root', '');
} catch(Exception $e) {
 exit('failed');
}

$sql = "sql insert into query";

$q = $bdd->prepare($sql);
$q->execute(array(':title'=>$title, ':typeConsult'=>$typeConsult ':start'=>$start, ':end'=>$end, ':finalConsult'=>$finalConsult, ':areHere'=>$areHere));
$query = "some sql select query";
$result = $bdd->query($query) or die(print_r($bdd->errorInfo()));
echo json_encode($result);

?>

Is the problem caused by me trying to post and get in the same php file ?
Or is it something else ?


Solution

  • The solution was that POST aren't allowed on json-server, so i migrated on a apache server that's way better (for all that came here and want the solution)