【问题标题】:PHP not receiving POST body set in Polymer core-ajax elementPHP 未接收 Polymer core-ajax 元素中设置的 POST 正文
【发布时间】:2015-03-30 11:11:07
【问题描述】:

我使用 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 数组是空的,而我希望它们被我提供的数据填充。

【问题讨论】:

    标签: javascript php ajax wordpress polymer


    【解决方案1】:

    所以我做了一些研究,得到了一些重要的发现。

    core-ajax 有一个 contentType 属性,默认设置为 'application/x-www-form-urlencoded'。

    我发送的数据是一个 json 对象,它是 不是 url 编码的。

    PHP 填充 $_POST 和 $_REQUEST 它包含 url-encoded 数据。 此外,与 GET 查询变量一样,数据必须采用 key=value[&key=value...] 对的形式,以便实例化 PHP 关联数组索引。

    所以我可以使用以下代码发送数据:

    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
    

    【讨论】:

      【解决方案2】:

      在这种情况下,您可能希望使用“params”属性this.$.login.params = '{"foo": 1, "bar": 2}'; 来代替“body”属性。这样做将允许您使用 $_POST 获取数据。以下 plunker 我用于另一个答案,显示了我如何在聚合物中创建表单,但也将在这里显示使用 php 将 $_POST 数据发送到后端。 http://plnkr.co/edit/JG7KKbMK0R1Fa1W2Gb4P?p=preview

      【讨论】:

        【解决方案3】:

        这是我使用 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
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-22
          • 1970-01-01
          • 1970-01-01
          • 2020-06-24
          相关资源
          最近更新 更多