简体   繁体   English

聚合物使用发布方法将数组发送到服务器

[英]Polymer send array to server using post method

I want to send some int array to server but I don't now how realize it. 我想向服务器发送一些int数组,但现在不知道如何实现。 I try to use core-ajax also I know about polymer-ajax but it element deprecated. 我也尝试使用core-ajax,但我也了解聚合物-ajax,但已弃用该元素。

I did something like this: 我做了这样的事情:

<template>
    <paper-fab on-tap="{{sendMethod}}"></paper-fab>
    <core-ajax id="post_ajax" 
      ...some attr...
      body={"some_array":"{{some_array}}"} 
    >
    </core-ajax>
</template>
<script>
        Polymer('el-name',{
            ready: function(){
                this.some_array = [];
            },
            sendMethod: function(){

                this.$.post_ajax.go();
            },
            someMeth: function(){
                this.some_array = data;
            }

        });
</script>

But data not riched server. 但是数据不能丰富服务器。 When I look debug I saw that array is not detected in body, but I try to get only one value: 当我看调试时,我发现体内没有检测到该数组,但是我尝试仅获取一个值:

body={"some_array":"{{some_array[0]}}"} 

value was detected but server not get value. 检测到值,但服务器未获取值。

I see that I 'm not use form but it seems like not need there or I m wrong. 我看到我没有使用表格,但似乎不需要表格,或者我错了。

UPDATE: 更新:

After some test I found some interesting. 经过一些测试,我发现了一些有趣的东西。 if I use BODY data not rich server, but if I use PARAMS data is rich server. 如果我使用BODY数据而不是富服务器,但是如果我使用PARAMS数据是富服务器。 It work on simple number value, but when I try send array, core-ajax is show error message in log. 它适用于简单的数字值,但是当我尝试发送数组时,core-ajax在日志中显示错误消息。 It say that params not understand simple array. 它说参数无法理解简单数组。 I think array need convert to json. 我认为数组需要转换为json。 I try to use stringfy but it not working. 我尝试使用stringfy,但是它不起作用。 May be I do something wrong? 可能我做错了吗? I have array consist simple numbers. 我有数组组成的简单数字。 Also my server side use PHP. 我的服务器端也使用PHP。

Try JSON.stringify(obj) to body content. 尝试将JSON.stringify(obj)转换为正文内容。

<core-ajax id="ajax" 
    method="POST"
    contentType='application/json'
    url="..."
    body='{{ajaxbody}}'
    handleAs="json" response="{{resp}}">
    </core-ajax>
....
  Polymer('x-elem', {
    ajaxbody:'',
    pobj:{foo:100,data:{x:200,y:120}},
    sendMethod: function(){
       this.ajaxbody = JSON.stringify(this.pobj);
       this.$.ajax.go();
    }
});

a jsbin example http://jsbin.com/vojuz/ jsbin示例http://jsbin.com/vojuz/

if the data shows in the request body of chrome dev console (or what ever dev console) then your error is server side. 如果数据显示在chrome开发控制台(或任何开发控制台)的请求正文中,则您的错误是服务器端。 if using php you can catch the request body with this command. 如果使用php,则可以使用此命令捕获请求正文。

 <?php $request_body = file_get_contents('php://input'); ?>

if using another language i can not help you. 如果使用另一种语言,我不能帮助您。 i had a issue creating polymer form and sending the data to a db for storage. 我在创建聚合物表格并将数据发送到数据库进行存储时遇到问题。 here is that post maybe it will help you. 这是该帖子,它可能会对您有所帮助。 polymer form using paper-input and core-ajax 使用纸张输入和芯轴的聚合物形式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM