Tianbo84
Tianbo84

Reputation: 321

Ajax not finding url

Quick question I have been working at but can't seem to get it fixed.

I have a ajax call but it is not getting a response from the php file, it seems to be loading a 404 instead.

The url in the address bar is "mydomain.com/checkcity/"

The location of the file with the ajax is "/php/advert/script.php"

The location of the php file to be called is "/php/advert/available.php"

I am using virtual urls through the use of htaccess.

Here is my ajax call:

    $.ajax({
        type: "POST",
        url: "/available.php",
        data: "city="+city,
        success: function(response){
        alert(response);
}
});

Here is my htaccess file:

    <IfModule mod_rewrite.c>
RewriteEngine On

RewriteBase /

RewriteRule ^index\.php$ - [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /index.php [L]

</IfModule>

One thing I noticed is when I search the url in the address bar it will load the php file just not with the ajax call.

I am running this currently on Apache 2.2 localhost

Thanks for any suggestions.

Upvotes: 4

Views: 5100

Answers (2)

Tianbo84
Tianbo84

Reputation: 321

Ok so I know this is outdated but I thought I would answer why this was happening. I moved on to other things and this was the last thing to do so I was forced to figure it out which actually only took about half hour.

Ajax wasn't reaching the url in chrome but would in firefox. I then found out that "adblock" was blocking the url - This was because the url had "advert" in it and as soon as I relocated and renamed the file, presto it worked.

So just in case someone finds themselves in a similar situation.

Thanks

Upvotes: 0

Steven
Steven

Reputation: 6148

I'm assuming that you're seeing the 404 error message in the error log/console for your browser? It should also be displaying the URL that is returning the 404 response which, given your request, would be: http://www.mysite.com/available.php.

Problem

The problem is that your ajax call contains a / at the start of the URL parameter. This signifies the root domain of the website (i.e. it points to http://www.mysite.com/available.php).

url: "/available.php",

Solution

Simply change the url parameter to one of the following correct URLs

url: "http://www.mysite.com/php/advert/available.php",

OR

url: "/php/advert/available.php",

OR

url: "available.php",

OR

url: "./available.php",

Code for clarification

$.ajax({
    type: "POST",
    url: "/php/advert/available.php",
    data: "city="+city,
    success: function(response){
        alert(response);
    }
});

Upvotes: 4

Related Questions