簡體   English   中英

PHP沒有在Polymer core-ajax元素中接收POST主體集

[英]PHP not receiving POST body set in Polymer core-ajax element

我使用core-ajax創建了一個Polymer元素,通過POST將數據發送到PHP腳本。

<polymer-element name="login-process">
  <template>
    <core-ajax auto method="POST" id="login" url="/login.php" response="{{response}}">
    </core-ajax>
    <button on-tap={{doSend}}>send</button>
  </template>
<script>
Polymer('login-process', {
  ready: function() {
  },
  doSend: function() {
    this.$.login.body = '{"foo": 1, "bar": 2}';
    this.$.login.go();
  },
  responseChanged: function(oldValue) {
      console.log(oldValue);
      console.log(this.response);
  }
})
</script>
</polymer-element>

我檢查了HTTP請求,並驗證了數據是否正在發送。 但是 ,我的PHP腳本 - 另一方面 - 沒有顯示$ _POST或$ _REQUEST變量中的數據痕跡。 這是我的PHP腳本的內容:

header("Cache-Control: no-cache, must-revalidate"); //HTTP 1.1
header("Pragma: no-cache"); //HTTP 1.0
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past

error_reporting(E_ALL);
ini_set("display_errors", 1);
define("BASE_PATH", $_SERVER['DOCUMENT_ROOT']);

define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH . '/wp-load.php');


var_dump($_POST);
var_dump($_REQUEST);

$creds = array();
$creds['user_login'] = 'example';
$creds['user_password'] = 'plaintextpw';
$creds['remember'] = true;
$user = wp_signon( $creds, false );

if ( is_wp_error($user) ) {
    header("HTTP/1.0 401 Unauthorized");
    echo $user->get_error_message();
    exit();
}

header("HTTP/1.0 200 OK");

exit();

我知道,代碼不是很優雅。 但是我從腳本得到的回應是:

array(0){} array(0){} 錯誤 :用戶名無效。 忘記密碼?

所以$ _POST和$ _REQUEST數組是空的,當我期望它們被我提供的數據填充時。

所以我做了一些研究,並做了一些重要的發現。

core-ajax有一個屬性contentType默認情況下設置為'application / x-www-form-urlencoded'。

我發送的數據是一個json對象,它不是 url編碼的。

當PHP包含url編碼數據 ,PHP會填充$ _POST和$ _REQUEST。 此外,對於要實例化的PHP關聯數組索引,數據必須采用key = value [&key = value ...]對的形式,與GET查詢變量一樣。

所以我可以使用以下代碼發送數據:

this.$.login.body = 'data='+encodeURIComponent(JSON.stringify({"foo":1, "bar":2}));

現在在PHP腳本中:

echo $_POST['data'];  // outputs something like string(32) "{\"foo\":1, \"bar\":2}"

var $unescaped = stripslashes($_POST['data']);

echo $unescaped; // outputs string(29) "{"foo":1, "bar":2}"

$assoc_array = json_decode($unescaped, true); // now contains an assoc array of the
// json data

但是,我看到人們在他們的PHP腳本中使用以下代碼:

file_get_contents("php://input");

這會將原始POST數據發送到Web服務器,無論它是否是url編碼的。

所以,我可以輕松地繼續發送數據:

this.$.login.body = '{"foo": 1, "bar": 2}';

並使用PHP使用以下數據集:

$data = json_decode( file_get_contents( "php://input", true ) );
echo $data["foo"]; // outputs 1

以下是我使用core-ajax來發布一些數據的方法:

在模板中:

<core-ajax
        method="POST"
        id="core_ajax_el"
        on-core-response="{{ajax_get_responseHandler}}"
        >
</core-ajax>

在原型中:

    domReady: function(e) {

    //set AJAX request params: they will be POSTed to the ajax-url.
    var core_ajax_el = this.$.core_ajax_el;

    // create the object literal
       var aniArgs = {};
       aniArgs['post_data'] = this.element_attribute;
       core_ajax_el.params = aniArgs;

       //set the AJAX URL
         core_ajax_el.url = this.ajax_get_url;

       //execute AJAX
         core_ajax_el.go();

    },

 ajax_get_responseHandler: function(e, detail, sender) {
   //do something with the response here

}

在這種情況下,您可能希望使用“params”屬性this.$.login.params = '{"foo": 1, "bar": 2}'; 代替“body”屬性。 這樣做可以讓你用$ _POST獲取數據。 以下plunker我用於另一個答案,顯示我如何在聚合物中創建一個表單,但也將在這里工作顯示使用PHP將$ _POST數據發送到后端。 http://plnkr.co/edit/JG7KKbMK0R1Fa1W2Gb4P?p=preview

暫無
暫無

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

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