【问题标题】:Why am I unable to post a JS object to PHP successfully?为什么我无法成功将 JS 对象发布到 PHP?
【发布时间】:2016-05-10 01:04:28
【问题描述】:

我研究了许多 SO 问题,并遵循了回答者的建议,包括 this post,但我仍在努力让它发挥作用。我准备了一个简化的测试。

JS & HTML

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
        <script>
            $(function () {
                data = {};
                data.subject = "Change the subject";
        /*
                $.post('php/test.php', { jsonData: JSON.stringify(data) }, function (response) {
                    console.log($(response).html());
                });
        */
                $.ajax({
                    url: 'php/test.php',
                    data: data,
                    contentType: 'application/json',
                    cache: false
                }).done(function (result, success, xhr) {
                    console.log("result=" + result);
                }).fail(function (xhr, desc, err) {
                    var msg = "Send mail - " + "\nError: " + "\n" + xhr.responseText;
                    console.log(msg);
                });
            });

        </script>
    </head>
    <body>
        <form id="form" name=  "form" >
            <input id="subject" name="subject" value="not a subject" />        
        </form>
    </body>
</html>

PHP

编辑

我将 $_POST 更改为 $_REQUEST - 没有帮助

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

/*
$arr=json_decode($_POST['jsonData']);
echo $arr[0]['id'];
print_r($arr);
*/
    if (!isset($_POST["subject"]) || empty(trim($_POST["subject"])))
        throw new Exception('A subject is required.');       
    else $subject = filter_var(trim($_POST["subject"]), FILTER_SANITIZE_STRING);

    echo "subject=" . $subject;

console.log

test.html:22 result=
( ! ) 致命错误:未捕获的异常 带有消息“需要主题”的“例外”。在 /home/deje/public_html/writers-tryst/php/test.php 上线 12 ( ! ) 例外:需要一个主题。在 /home/deje/public_html/writers-tryst/php/test.php 上线 12 调用堆栈#TimeMemoryFunctionLocation 10.0003248416{main}( ).../test.php:0

附加错误日志:

[10-May-2016 01:50:45 UTC] PHP 致命错误:未捕获的异常 带有消息“需要主题”的“例外”。在 /home/deje/public_html/writers-tryst/php/test.php:12 堆栈跟踪:

0 {main} 在第 12 行的 /home/deje/public_html/writers-tryst/php/test.php 中抛出

编辑

网络表:

【问题讨论】:

  • 你为什么要评论/* $.post('php/test.php'... */?您的 (POST) 数组依赖于它。否则,它默认为 GET。或在您的表单中指定它&lt;form id="form" name= "form" method="post"&gt;
  • 那么请告诉我们什么错误报告给您带来了影响?顺便说一句,您在下面有一个答案,如果这是问题所在,那么您的问题中没有包含该错误。
  • 我注释掉了 $.post 因为我尝试过它,它产生的结果与我添加方法/类型后提供的答案相同:POST。 error_log 报告了我发布的相同错误信息(我也在发布错误日志)。我没有放弃这个问题,我有一些紧迫的事情。在我看来,这与提到的问题没有重复。
  • 通过在 if 语句前添加 print_r($_POST); die; 来检查 $_POST 内容。所以你可以看到你实际发送到 php 文件的内容。
  • 谢谢,@iamvain2。我学到了一些东西。据说,如果你不笑,不学东西,那一天就白费了

标签: javascript php jquery ajax


【解决方案1】:

$.ajax的默认method为GET,改为POST

         $.ajax({
                    url: 'php/test.php',
                    data: data,
                    method: 'post',
                    cache: false
                }).done(function (result, success, xhr) {
                    console.log("result=" + result);
                }).fail(function (xhr, desc, err) {
                    var msg = "Send mail - " + "\nError: " + "\n" + xhr.responseText;
                    console.log(msg);
                });

【讨论】:

  • 顺便说一下,属性是“类型”而不是“方法”e
  • 根据 Jquery 文档 (api.jquery.com/jquery.ajax):类型是方法的别名。如果您使用的是 1.9.0 之前的 jQuery 版本,则应该使用 type。
  • 事实证明你是对的。我也确实将数据对象也更改为序列化。
【解决方案2】:

在test.php中

$subject = array();
$subject[] = 'assign values';
echo json_encode($subject);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-11
    • 2014-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多