铁ajax没有将输入数据发布到php

时间:2015-12-30 21:10:01

标签: javascript php ajax polymer polymer-1.0

聚合物元素脚本的一部分:

<content>
            <iron-ajax id="plWhois" method="POST" body='{"helper":"plugin", "func":"_pluginsInitAjax", "params":{"domain": "domain", "request_type": "taken"}}' handle-as="json" on-response="handleResponse" debounce-duration="300"></iron-ajax>
            <paper-card heading="Begin the search for your perfect domain name..." image="themes/custom_components/apps/pl-whois/img/who-is.jpg" center>
                <div class="card-content">
                    <paper-input id="plWhoisSearchBtn" type="search" placeholder="e.g. mydomain.com">
                        <iron-icon prefix icon="search"></<iron-icon>
                    </paper-input>
                </div>
                <div class="card-actions s-t">
                    <paper-button on-click="domainAvailability">Search</paper-button>
                    <paper-button>Transfer</paper-button>
                </div>
                <div class="card-actions p-l">
                    <paper-button>buy a domain</paper-button>
                    <paper-button>order hosting</paper-button>
                    <paper-button>make payment</paper-button>
                    <paper-button>support</paper-button>
                </div>
            </paper-card>
  </content>

<script>
        // element registration
        Polymer({
            is: "pl-whois",
            properties: {
                url: {
                    type: String,
                                notify: true,
                    value: ''
                },
                body: {
                    type: Object,
                                notify: true,
                    value: ''
                }
        },
            domainAvailability: function () {
                var domain = this.$.plWhoisSearchBtn.value;

                this.$.plWhois.url = "ajax.php";
                /*this.$.plWhois.body = {"helper":"plugin", "func":"_pluginsInitAjax", "params":{"domain": domain, "request_type": "taken"}};
                */
                this.$.plWhois.generateRequest();
            },
            handleResponse: function(e) {
                console.log(e.detail.response);
            }
        });
    </script>

问题是,上面的元素是导入的,我使用以下方法从聚合物中获取发布数据:

  

$ json = file_get_contents(&#34; php:// input&#34;);

     

$ _ POST = json_decode($ json,true);

我得到了here

无导入的脚本工作正常,他们发布数据,但导入的脚本执行ajax请求但不发布数据到php。

2 个答案:

答案 0 :(得分:2)

您似乎错过了url来电的<iron-ajax>属性。

答案 1 :(得分:0)

我仍然遇到同样的问题,看起来像是一个bug或者什么东西,(动态导入的脚本在铁-ajax请求期间不会发送帖子数据)。

因此,作为一种解决方法,从元素的属性中获取url和body值,然后将它们绑定到iron ajax似乎有效:

<强> 铁AJAX:

  

&LT; iron-ajax id =&#34; plWhois&#34;方法=&#34; POST&#34; URL =&#34;的 {{URL}} &#34;体=&#39;的 {{PARAMS}} &#39;处理-AS =&#34; JSON&#34;上响应=&#34;用handleResponse&#34;去抖动持续时间=&#34; 300&#34;&GT;&LT; /铁AJAX&GT;

此脚本将属性添加到我的元素并触发铁请求:

var domain = this.$.plWhoisSearchBtn.value;

this.url = 'ajax.php';
this.params = '{"helper":"plugin", "params":{"0":"whoapi_dot_com", "1":"_test", "2":{"0": "'+domain+'", "1": "taken"}}}';     
this.$.plWhois.generateRequest();

现在这就是我导入的元素的样子(部分):

//imports
<dom-module id="pl-whois" class="wide-layout-mq">
    <template>
  <style></style>
      <iron-ajax id="plWhois" method="POST" url="{{url}}" body='{{params}}' handle-as="json" on-response="handleResponse" debounce-duration="300"></iron-ajax>

  <content>
            <paper-card heading="Begin the search for your perfect domain name..." image="themes/custom_components/apps/pl-whois/img/who-is.jpg" center>
                <div class="card-content">
                    <paper-input id="plWhoisSearchBtn" type="search" placeholder="e.g. mydomain.com" on-keyup="updateParams">
                        <iron-icon prefix icon="search"></<iron-icon>
                    </paper-input>
                </div>
                <div class="card-actions s-t">
                    <paper-button on-click="domainAvailability">Search</paper-button>
                    <paper-button>Transfer</paper-button>
                </div>
                <div class="card-actions p-l">
                    <paper-button>buy a domain</paper-button>
                    <paper-button>order hosting</paper-button>
                    <paper-button>make payment</paper-button>
                    <paper-button>support</paper-button>
                </div>
            </paper-card>
  </content>
</template>

<script>
    // element registration
    Polymer({
        is: "pl-whois",
        properties: {
            url: {
                type: String,
                notify: true,
      reflectToAttribute: true,
                value: ''
            }
    },
  updateParams: function (e) {
    var domain = this.$.plWhoisSearchBtn.value;

    this.url = 'ajax.php';
            this.params = '{"helper":"plugin", "params":{"0":"whoapi_dot_com", "1":"_test", "2":{"0": "'+domain+'", "1": "taken"}}}';

    if(e.keyCode === 13){
      this.$.plWhois.generateRequest();
    }
        },
        domainAvailability: function () {
    var domain = this.$.plWhoisSearchBtn.value;

    this.url = 'ajax.php';
            this.params = '{"helper":"plugin", "params":{"0":"whoapi_dot_com", "1":"_test", "2":{"0": "'+domain+'", "1": "taken"}}}'; 

            this.$.plWhois.generateRequest();
        },
        handleResponse: function(e) {
            console.log(e.detail.response);
        }
    });
</script>

希望这可以帮助那些陷入困境的人!