Skip to content
Advertisement

How to make Ajax POST on PHP MVC using friendly urls?

I’m trying my first PHP MVC project from scratch. Everything is working fine… everything but ajax calls. When I try to POST I get a 404 error, but ONLY using AJAX; I’ve made the same calls using html form and it works. This is my request:

$.ajax({
    type: "get",
    url: "/Prueba/Bodega/createPost",
    data: data,
    success: function (data) {

and this is on my view (works if I comment the “preventDefault” line on js)

 <form action="<?=BASE_URL?>Bodega/createPost" method="POST">

Both methods calls the controller Bodega, function createPost (“http://localhost/prueba/Bodega/createPost“). I think this is because of the .htaccess file using friendly urls but I coundn’t get the solution.

Advertisement

Answer

This caused because the we server treats the request URLs in a case sensitive way. That said:

/Prueba/Bodega/createPost and /prueba/Bodega/createPost are two different URLs.

Please change /Prueba/Bodega/createPost to /prueba/Bodega/createPost and it should get fixed.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement