PHP没有收到POST体聚合物核心AJAX元素设置聚合物、元素、核心、PHP

2023-09-10 20:24:32 作者:脚踩月亮下

我已经创建使用核心AJAX通过POST将数据发送到一个PHP脚本的聚合物组成部分。

I've created a Polymer element using core-ajax to send data via POST to a PHP script.

<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脚本的内容:

I've examined the HTTP request, and verified the data is being sent. However, my PHP script - on the other hand - is not showing traces of the data in the $_POST or $_REQUEST variables. This is the contents of my PHP script:

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();

不优雅code,我知道了。但是,我从剧本得到的回应是:

Not elegant code, I know. But the response I get from the script is:

阵列(0){}阵列(0){} 错误:无效的用户名。迷失   你的密码?

array(0) { } array(0) { } ERROR: Invalid username. Lost your password?

所以,$ _ POST和$ _REQUEST数组是空的,当我希望他们充满了我提供的数据。

So the $_POST and $_REQUEST arrays are empty, when I'm expecting them to be filled with the data I've provided.

推荐答案

在这种情况下,你会想用PARAMS属性这$ login.params ='{富。: 1,巴:2}; 到位身体的属性。这样做将使你得到的数据与$ _ POST。以下plunker我用另一种答案展示如何创建聚合形式,但也将在这里显示发送$ _ POST数据与PHP的后端。 http://plnkr.co/edit/JG7KKbMK0R1Fa1W2Gb4P?p=$p$pview

in this case you would want to use the "params" attribute this.$.login.params = '{"foo": 1, "bar": 2}'; in place of the "body" attribute. doing so would allow you to get the data with $_POST. the following plunker i used for another answer showing how i create a form in polymer but will also work here to show sending $_POST data to a backend with php. http://plnkr.co/edit/JG7KKbMK0R1Fa1W2Gb4P?p=preview