jQuery AJAX Post无法正常工作

问题描述:

I cant not figure out why this script doesnt work. It has to do with something within the AJAX POST script/function. Right now, when I hit submit on my form it runs the php code on the same page. What it should do it is send the values of the form to project_ajax.php, then at page will return a var of success that will be true or false.

$(document).ready(function () {
    $('div#didIt').hide();
    $('form[name=adminForm]').submit(function () {
        $.post('/project_ajax.php', {
            action: $('[name=action]').val(),
            pId: $('[name=pId]').val(),
            name: $('[name=name]').val(),
            url: $('[url=url]').val(),
            summary: ('[summary=summary]').val()
        }, function (data) {
            if (data.success) {
                $('div#didIt').slideDown('slow');
            } else {
                alert('Failed SA!');
            }
        }, 'json');
        return false;
    });
});

Below is what the code for project_ajax.php...

if($_POST['action'] == "update") {
    //Prep the field for Query Entry!-----------------------------------
    $pId     = $_POST['pId'];
    $name    = trim(mysql_prep($_POST['name']));
    $status  = 1;
    $url     = trim(mysql_prep($_POST['url']));
    $tumb    = false; //False because I still need to make a script for it.
    $summary = trim(mysql_prep($_POST['summary']));
    $creater = $_SESSION['userId'];
    $created = date("Ymd");

    $q = "UPDATE " . DB_NAME . ".`projects` SET 
                    name    = '{$name}',
                    status  = '{$status}',
                    url     = '{$url}',
                    summary = '{$summary}',
                    creater = '{$creater}',
                    created = '{$created}'
              WHERE `projects`.`id` = {$pId}";
    $r = mysql_query($q, $connection);
    if ($r) {
        //Successful
        $data['success'] = true;
        $date['error']   = false;
        $date['message'] = "You are the Greatest!";

    } else {
        //Fail
        $data['success'] = false;
        $data['error']   = true;
        $date['message'] = "You can't do it fool!";

    }

} else {
    $data['success'] = false;
    $date['error']   = true;
    $data['message'] = "You Failed Stupid!";
}

echo json_encode($data);

Note: When load this page. The browser never really finsh loading. The the blue-ring on the title tab within IE8 spins as if the page never finsh loading.

On this line:

  summary: ('[summary=summary]').val()},

You have a missing $ which represents the jQuery function. Corrected:

summary: $('[summary=summary]').val()},

Because you have a Javascript error, the execution is terminated. Thus the loading takes forever.