1

I am doing a ajax call , but its giving a error saying 404 file not found.

        $.ajax({ 
            type      : 'POST',
            url       : 'app/lib/functions.php',
            data      : data,
            success   : function(data) {

            }
        });

My project struc is this

enter image description here

I am doing a ajax call from main.js (in public fodler) to functions.php (in app folder). Please help. I am tired of this. Edited : I tried with ../ and ../../ in front of the url in ajax but it does not work. I think its something to do with app folder , as this folder is not available to client side , something like that. I dont know how to do the ajax now. My .htaccess has this code as I am redirecting to public folder by default.

RewriteEngine On
RewriteBase /proj_name/

RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]

RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

EDIT : If I remove the .htaccess , '../app/lib/functions.php' path works

5
  • Is your site being hosted by a server, or running on a local server? Or are you just loading the page in a browser as a local file without any server? Commented Sep 30, 2019 at 18:35
  • @IceMetalPunk I am using localserver Commented Sep 30, 2019 at 18:49
  • did you try with "/app/lib/function.php"? Commented Sep 30, 2019 at 20:25
  • @FélixBrunet yes Commented Sep 30, 2019 at 20:45
  • I think you need to have your php file inside your public folder. Check this answer by Quentin stackoverflow.com/questions/21377416/jquery-ajax-url-not-found Commented Oct 1, 2019 at 13:00

2 Answers 2

0

maybe try it with:

$.ajax({ 
        type      : 'POST',
        url       : '../../app/lib/functions.php',
        data      : data,
        success   : function(data) {

        }
    });

or

$.ajax({ 
        type      : 'POST',
        url       : '../app/lib/functions.php',
        data      : data,
        success   : function(data) {

        }
    });
Sign up to request clarification or add additional context in comments.

Comments

0

I don't use AJAX much but I believe it should be pointing to a route, not directly to a file. That path would be relative and never actually resolve.

3 Comments

so what should i do
Can you turn that directory /app/lib/functions.php into a route, so youd be accessing <baseurl>/functions and sending post data. This way it is accessible much like an API and dynamic and can be called the same from anywhere. it also seems like you are calling the whole file when I am assuming you may only need a single function from it.
You can use the DIR variable and append to route to the file lik DIR . ../.. or wherever you want to go

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.