简体   繁体   中英

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. I try to use core-ajax also I know about polymer-ajax but it element deprecated.

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. It work on simple number value, but when I try send array, core-ajax is show error message in log. It say that params not understand simple array. I think array need convert to json. I try to use stringfy but it not working. May be I do something wrong? I have array consist simple numbers. Also my server side use PHP.

Try JSON.stringify(obj) to body content.

<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/

if the data shows in the request body of chrome dev console (or what ever dev console) then your error is server side. if using php you can catch the request body with this command.

 <?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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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