Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - ajax url not found

Tags:

jquery

ajax

php

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...


1 Answers

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.

like image 180
Quentin Avatar answered Nov 20 '25 07:11

Quentin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!