簡體   English   中英

AJAX請求后,帶有JSON解碼的PHP表單出現錯誤

[英]Error on PHP Form with json decode after AJAX Request

經過大量研究和困惑,我決定在這里提出要求。

基本上,除了JSON之外,我可以通過各種方式發布AJAX請求。 我想學習它,所以我可以從JSON開始而不是一路學習。

PHP代碼如下錯誤

未定義索引:數據

我想念什么?

這是.serializeArray()和JSON Stringyfied控制台字符串:

[
    {
        "name" : "client_id",
        "value" : "111"
    }, {
        "name" : "project_id",
        "value" : "222"
    }, {
        "name" : "user_id",
        "value" : "465605"
    }, {
        "name" : "utl_latitude",
        "value" : "40.6110589"
    },{ 
        "name":"utl_longitude",
        "value":"-111.8999353"
    },{
        "name" : "utl_event",
        "value":"CLOCK IN"
    }, {
        "name" : "utl_action",
        "value" : "FOLLOW UP"
    }
]

據我所知, AJAX請求

// START AJAX REQUEST ---------------------------------------------------------------------------        
$.ajax({

    url: 'test.php',
    method: 'POST',
    dataType: 'json',
    data: url_parameters,
    //contentType: "application/json; charset=utf-8",
    timeout: 5000, // Longer than 5 seconds? HTTP SERVER or PHP Offline.***

    beforeSend: function () {

        self.status_please_wait();

    },

    success: function (data, textStatus, jqXHR) {

        /* PHP PDO MySQL Insert Status --------------------------------------------------

        return 0 = Nothing to Update (***Disable for Select & Insert)
        return 1 = Successful Insert Query
        return 2 = Database Connection refused
        return 3 = MySQL Query Error OR Wrong URL Parameters */

        // ----------------------------------------------------------------------------

        // If data return 0 = Nothing to Update
        // Placeholder for future mysql Update

        // ----------------------------------------------------------------------------
        // If data return 1 = Successful Insert Query
        if (textStatus === 'success' && data === '1') {

            //self.php_pdo_mysql_insert_status(data);

            self.status_insert_success();

        }

        // ----------------------------------------------------------------------------
        // If data return 2 = Database Connection refused
        if (textStatus === 'success' && data === '2') {

            //self.php_pdo_mysql_insert_status(data);

            self.status_mysql_connection_refused();

        }

        // ----------------------------------------------------------------------------
        // If data return 3 = MySQL Query Error OR Wrong URL Parameters
        // Placeholder for future mysql error 3

        // ----------------------------------------------------------------------------

    },
    // END SUCCESS function ---------------------------------------------------------

    error: function (jqXHR, textStatus, errorThrown) {
            // NIGX offline or PHP offline.

            // If http server and/or PHP is/are offline...
            if (errorThrown === 'timeout') {

                self.status_http_php_offline();

            }


        }
        // END ERROR function ---------------------------------------------------------

});
// END AJAX REQUEST -----------------------------------------------------------------------------

服務器端的PHP:

<?php

    $decoded = json_decode($_POST['data'],true);

    foreach ($decoded as $value) {
       echo $value["name"] . "=" . $value["value"];
    }

?>

PHP錯誤


注意 :未定義的索引:第9行的F:\\ Wnmp \\ html \\ mysql_post_json \\ test.php中的數據

警告 :在第11行的F:\\ Wnmp \\ html \\ mysql_post_json \\ test.php中為foreach()提供了無效的參數

最后,答案。

<?php

// First get raw POST input
$raw_post     = file_get_contents('php://input');
// Run through url_decode..
$url_decoded  = urldecode($raw_post);
// Run through json_decode...
$json_decoded = json_decode($url_decoded,true);

// Finally access it with foreach
// foreach {"name":"client_id","value":"111"},
//         {"name":"project_id","value":"222"},...
// $json_decoded = {"name":"client_id","value":"111"} each individual of these JSON formated key/value pairs.
foreach ($json_decoded as $column) {
    // Access them.
    // client_id=111
    // project_id=222
   echo $column["name"] . "=" . $column["value"];

}

?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM