Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the best way to debug AJAX to PHP calls?

I'm having a miserable time debugging one small function on my new project.

Essentially I'm having a user log out via an AJAX call to my log out script on my server called "userfFunctions.php" I'm using AJAX so that I don't have the headache of writing more regex to match my mod_rewrites. Anyway, every so often, it seems as though my Post data just flat out dies and since the PHP is running behind the scenes, I feel like I have no way of finding out where the data flow is being disrupted. BTW This function works 19 hrs of the day.

Here is the javascript function:

function logOut(){
    var data = new Object;
    data.log_out = true;
    $.ajax({
        type: 'POST',
        url: 'http://www.mydomain.com/User_Validator', //<-- redirects to userFunctions.php
        data: data,
        success: function(data) {
        alert(data); // <-- a response is triggered but with no response data!
        }
    });
}

the php side:

if(isset($_POST['log_out'])){
     echo 'alert this!';
}

here is my awesome response: alt text http://img517.imageshack.us/img517/6520/screenshot20100517at443.png

like image 968
Howard Zoopaloopa Avatar asked May 17 '10 23:05

Howard Zoopaloopa


3 Answers

FirePHP:

FirePHP enables you to log to your Firebug Console using a simple PHP method call.

All data is sent via response headers and will not interfere with the content on your page.

FirePHP is ideally suited for AJAX development where clean JSON and XML responses are required.

Here is a minimalistic implementation I wrote:

function FirePHP($message, $label = null, $type = 'LOG')
{
    static $i = 0;

    if (headers_sent() === false)
    {
        $type = (in_array($type, array('LOG', 'INFO', 'WARN', 'ERROR')) === false) ? 'LOG' : $type;

        if (($_SERVER['HTTP_HOST'] == 'localhost') && (strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP') !== false))
        {
            $message = json_encode(array(array('Type' => $type, 'Label' => $label), $message));

            if ($i == 0)
            {
                header('X-Wf-Protocol-1: http://meta.wildfirehq.org/Protocol/JsonStream/0.2');
                header('X-Wf-1-Plugin-1: http://meta.firephp.org/Wildfire/Plugin/FirePHP/Library-FirePHPCore/0.3');
                header('X-Wf-1-Structure-1: http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1');
            }

            header('X-Wf-1-1-1-' . ++$i . ': ' . strlen($message) . '|' . $message . '|');
        }
    }
}

I wrote it so that it only works on localhost (for security reasons), but you can easily change that by replacing the following code:

if (($_SERVER['HTTP_HOST'] == 'localhost') && (strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP') !== false))

With:

if (strpos($_SERVER['HTTP_USER_AGENT'], 'FirePHP') !== false)
like image 193
Alix Axel Avatar answered Oct 23 '22 13:10

Alix Axel


Have you tried setting the dataTypeto "text"?

function logOut(){
    var data = {
        "log_out" : true
    };
    $.ajax({
        type: 'POST',
        url: 'http://www.mydomain.com/User_Validator',
        data: data,
        success: function(data) {
            alert(data);
        },
        dataType : 'text'
    });
}

Also, I would change your PHP to this:

print_r($_POST);
like image 44
nickf Avatar answered Oct 23 '22 12:10

nickf


Try using something like the FireBug plugin for Firefox, or the Developer Tools in Chrome, to look at the request being sent out.

like image 8
Amber Avatar answered Oct 23 '22 12:10

Amber