开发者

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


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)


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


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);


I noticed:

//<-- redirects to userFunctions.php

When you do a redirect ( header("Location: "); ) you'll lose your $_POST and $_GET data. (If you're referring to a url rewrite (with mod_rewrite) you should receive the data.)

But this doesn't explain the 19hr symptom.


You could try checking the $_POST array itself, with something like this:

var_dump($_POST);

See if the array is even being populated at all and then work from there. Using firebug, you can also confirm if the AJAX post is truly sending data (check the console or net tabs).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜