3

Ok, I'm sure this is really easy and I'm being stupid, but just can't seem to get the bottom of it.

I am trying to make a simple AJAX call to some code in "helpers.php" from my js file called "custom.js". However, I keep getting a 404 error as I don't appear to be traversing the folders correctly, although I'm convinced I am...

The folder structure I have is as follows:

html
    index.php
    js/
        custom.js
includes
    helpers.php

And the code I'm using in JS:

$(document).on('ready', function() {
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "../../includes/helpers.php",
        data: { func: "results", days: "7"}, 
        success: function(rows) {
            console.log(rows);
        }
    });
});

but in the console I get:

The requested URL /includes/helpers.php was not found on this server.

Where am I going wrong, any pointers appreciated...

5
  • Your above code is in index.php ? Commented Jan 27, 2014 at 9:42
  • Have you tried using ../includes/helpers.php as an url? I thing that should work. Commented Jan 27, 2014 at 9:42
  • May it depends on where the JS was used from, not where the JS is actually placed? Commented Jan 27, 2014 at 9:42
  • where is your js included..on the page.give location from there.. Commented Jan 27, 2014 at 9:43
  • I include the "custom.js" in the index.php file... Commented Jan 27, 2014 at 9:43

7 Answers 7

6

You appear to have two problems:

JavaScript is executed in the context of the document. One of the effects of this is that, unlike CSS, all URLs are relative to the document not the .js file. You have one ../ too many.

You are trying to access a private PHP file

html is, presumably, the DocumentRoot of your site. Files outside it do not get URLs (if they did, then any file on your hard disk would be visible to the WWW).

Your directory structure suggests that your code organisation is such that you should be creating /html/ajax/something.php which includes helpers.php and calls functions in it.

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

7 Comments

thanks for the answer. I get what you mean, and I thought as much and edited the url to be ../includes/helpers.php but I'm still getting a 404. Is that a case of something in the code being wrong or something else? permissions of the helpers.php file are 644 so be accessible..?
@m0atz — See the second half of the answer, starting "You are trying to access a private PHP file"
I get it. in fact, if I move the php file inside the html folder it works with the change to the url. I get it now! thanks!!
@Quentin includes/helpers.php is correct or wrong ??? because the js file is included in index.php
@susheel — Since includes is not a subfolder of html, it is wrong.
|
1

change url to

url: "../includes/helpers.php",

since you have included the js to index file the path to the root will relative to the index.php not to the custom.js (I hope my statement is not confusing)

1 Comment

as above, I get the issue now is with the folder structure
1

Just like PHP includes, including a Javascript file in the script tag is the same as copy pasting the Javascript code at that part where the

So change it to:

    //Previous code...
    $.ajax({
            type: "POST",
            dataType: "json",
            url: "../includes/helpers.php",

   //Folowing code...

Comments

1

Your url in ajax relative to your index.php file.

Comments

0

I'm assuming your js code is called from index.php, therefore executed there.

The path should be relative from this page.

$(document).on('ready', function() {
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "../includes/helpers.php",
        data: { func: "results", days: "7"}, 
        success: function(rows) {
            console.log(rows);
        }
    });
});

2 Comments

What is your server documentRoot ?
Then, as @Quentin said, your helpers.php is outside of your server. You can't reach it, since html is the root. You need to re-think your architecture.
-1

Alternatively, you can probably use a static URL as well:

$(document).on('ready', function() {
    $.ajax({
        type: "POST",
        dataType: "json",
        url: "/includes/helpers.php",
        data: { func: "results", days: "7"}, 
        success: function(rows) {
            console.log(rows);
        }
    });
});

1 Comment

From the question: The requested URL /includes/helpers.php was not found on this server.
-1

Have you tried with putting custom.js and helpers.php in same folder..Check if it works or not

1 Comment

why will he put it in same folder. he wants some differenciation between client and server code. so includes/helpers.php will work

Your Answer

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