简单的jquery .post不返回数据


simple jquery .post not returning data

jquery语句:

$.post ('test.php', {f1: 'abc', f2: 'def'}, function (data) {console.log (data)})

测试.php:

 <?php
        $f1 = $_REQUEST ['f1'];
        $f2 = $_REQUEST ['f2'];
        echo $f1 . $f2;
?>

调用 jquery 语句后,不会返回任何内容,这些内容应该在控制台区域中看到。

但是,运行测试.html,浏览器按预期显示"f1f2"。 测试.html:

<form action='test.php' method="post">
    <label>f1</label>
    <input name="f1" type="text">
    <label>f2</label>
    <input name="f2" type="text">
    <input type="submit">
</form>

为什么 jquery post 请求没有返回数据?

这应该有效:

<?php
        $f1 = $_POST ['f1'];
        $f2 = $_POST ['f2'];
        echo $f1 . $f2;
    ?>

如果你想使用 jquery 发帖,不要提交你的表单,在提交时阻止默认,并触发帖子提交。

$("#yourFormId").submit(function(e){e.preventDefault();$.post ('test.php', {f1: 'abc', f2: 'def'}, function (data) {console.log (data)})})