JavaScript不会将数据发送到PHP脚本

时间:2013-11-24 20:22:18

标签: javascript php jquery ajax json

Firebug写道,此脚本的请求时间为0,add_comment_processor.php返回NULL。

$(document).ready(function(){
            $("#send_button").click(function(){
                var request = {
                    'comment': $("#new_comment_textfield").val(), //Value of textfield
                    'anonymously': $(".anonymously").is(':checked') //Value of anonymous check-button
                }
                $.ajax({
                    url: 'add_comment_processor.php',
                    type: 'post',
                    dataType: 'json',
                    success: function (data) {
                        var json = $.parseJSON(data);
                        alert(json); //returns NULL, but firebug writes that reques time of this script is 0ms
                    },
                    data: request //name of my object
                });
            });
        });

add_comment_processor.php
它只返回收到的数据,因此不会引起任何问题。

<?php
unset($authorized_user);
session_save_path($_SERVER['DOCUMENT_ROOT'].'\session');
session_start();

$authorized_id=$_SESSION['authorized_user']; //logged user

// Read from POST
$recieve_data = file_get_contents('php://input');

// decode JSON
$object = json_decode($recieve_data);

$comment=$object->comment;
$anonymously=$object->anonymously;

$send_array=array(
    'comment'=>$comment,
    'anonymously'=>$anonymously
);

echo json_encode($send_array);
exit();

0 个答案:

没有答案