1

I'm trying to do an ajax call from a javascript file to a PHP file.

This is the code i have:

function testAjax()
    {
        $.ajax({
            type: 'GET',
            url: 'http://127.0.0.1:81/Gorillo/account/test',
            success: function(data)
            {
                alert("Success");
            },
            error: function(err)
            {
              console.log(JSON.stringify(err));
            }
        })

    }

The function in the PHP file:

function test()
{
    echo "hello";
}

In my browser console i get the following error upon running the code:

{"readyState":4,"status":404,"statusText":"error"}

However, when i use XDebug to debug the code, i can see the PHP code is being executed by placing breakpoints. This means the ajax call does find the PHP file/function.

Why do i get a 404 error?

Regards

k1u
  • 93
  • 2
  • 9
  • can you open the url in your browser? – Alex Andrei Nov 13 '15 at 09:32
  • 404 Error means the webserver could not complete the request because the file was not found. try opening the url in the browser. – Tanuel Mategi Nov 13 '15 at 09:32
  • 2
    is it possible that your php should be `....test.php` or `.....test/index.php`? – swidmann Nov 13 '15 at 09:33
  • When i visit the url in de browser, it shows the echo message. – k1u Nov 13 '15 at 09:34
  • 2
    Possible duplicate: http://stackoverflow.com/questions/8241911/jquery-ajax-dosent-work-with-http-127-0-0-15984 try `url: 'http://localhost:81/url` or simply `url: '/Gorillo/account/test'` – Tanuel Mategi Nov 13 '15 at 09:37
  • @TanuelMategi the url changes don't work. What do you mean with cross domain issue? It seems far fetched because i can make ajax calls from another javascript file perfectly fine. – k1u Nov 13 '15 at 09:48
  • This is what you need: http://stackoverflow.com/questions/2269307/using-jquery-ajax-to-call-a-php-function – skywalker Nov 13 '15 at 09:49
  • @skywalker I tried the things you suggested in the post, it still does the same. In the PHP file i now check what $_GET contains. When i debug my PHP code, the function still gets executed, but the call still returns 404. Could it be that i am not returning data the proper way? – k1u Nov 13 '15 at 10:04

0 Answers0