javascript - Ajax 调用语法

标签 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);
        ?>

最佳答案

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

关于javascript - Ajax 调用语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26595308/

相关文章:

javascript - 如何在 webgl 中移动相机?

javascript - 使用 JavaScript 查询 Sesame Triplestore

javascript - 判断字符串是像素还是百分比,然后除以二

jquery - 使用 AJAX 提交付款时 PayPal 500 错误 Java 空指针异常错误

javascript - Ajax 与 Node.js

Java Http POST ajax 相当于发送结构化数据 JSON

javascript - 当我在 javascript 文件中运行它但未嵌入 html 文件时,该函数有效

javascript - Chrome 似乎报告错误的错误行

jquery - 如何使用jquery通过所选选项值选择选择字段

jquery - 选项卡 - 突出显示事件选项卡