Ajax调用语法

时间:2014-10-27 19:24:50

标签: javascript jquery ajax

尝试做一个简单的ajax帖子,出于某种原因,它没有发布我的数据!它成功发布到文件(ajaxpost.php)但没有传递POST数据..

            var myKeyVals = {caption: "test"};
                $.ajax({ //Process the form using $.ajax()
                    type        : 'POST', //Method type
                    url         : 'ajaxpost.php', //Your form processing file url
                    data        : myKeyVals, //Forms name
                    dataType    : 'json',
                    success     : function(data) {

                    if (!data.success) { //If fails
                        if (data.errors.name) { //Returned if any error from process.php
                            $('.throw_error').fadeIn(1000).html(data.errors.name); //Throw relevant error
                        }
                    } else {
                            $('#success').fadeIn(1000).append('<p>' + data.posted + '</p>'); //If successful, than throw a success message
                        }
                    }
                });

这是我的AjaxPost.php ..

        <?php
        $text = $_GET['caption'];
        $file = 'people.txt';
        // Open the file to get existing content
        $current = file_get_contents($file);
        // Append a new person to the file
        $current = "LOG: " . $text . "\n";
        // Write the contents back to the file
        file_put_contents($file, $current, FILE_APPEND);
        ?>

2 个答案:

答案 0 :(得分:1)

在您的php文件中,您使用的是$_GET['caption'],但您应该使用$_POST['caption'],因为这是一个帖子请求。

答案 1 :(得分:1)

使用POST在PHP中访问

$_POST数据,而不是$_GET

或者,如果您希望同时支持两种HTTP方法,则可以使用$_REQUEST

相关问题