Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

polymer-ajax: properly serialize nested objects/arrrays #33

Open
ebidel opened this issue Oct 26, 2013 · 4 comments
Open

polymer-ajax: properly serialize nested objects/arrrays #33

ebidel opened this issue Oct 26, 2013 · 4 comments

Comments

@ebidel
Copy link
Contributor

ebidel commented Oct 26, 2013

Demo: http://jsbin.com/AVOTUTAC/1/edit

I'm not sure the right answer, but nested objects/arrays get serialized as strings when they're URL encoded by the element.

<polymer-ajax id="ajax" params="{{params}}" method="post" ...></polymer-ajax>
...
this.params = {
  credential: {
    username: "ebidel",
    password: "abc"
  }
}

produces:

...&credential=%5Bobject%20Object%5D

Instead, we could do basic parameterizing for arrays and objects...?

credential = [1,2,3] -> &credential0=1&credential1=2&credential2=3
credential = {username="ebidel", password:"abc"} -> &credential_ebidel=ebidel&credential_password=abc
@k33g
Copy link

k33g commented Dec 6, 2013

I have the same problem, i can set it with jQuery in this way :

    $.ajax({
      type:"POST", dataType: 'json', contentType: "application/json",
      url:"humans",
      data:JSON.stringify({username="ebidel", password:"abc"}),
      error : function(err){console.log(err);},
      success : function(data){
        //foo
      }
    });

@sjmiles
Copy link
Contributor

sjmiles commented Dec 6, 2013

What you want to do there is send a JSON body POST, using polymer-ajax it
looks like this:

this.$.ajax.method = 'POST' // could be declared on the element with method="POST"
this.$.ajax.xhrArgs = {body: JSON.stringify(data)};
this.$.ajax.go();*

There is a discussion of this topic here but it is mostly about usage in Dart.

Scott

@k33g
Copy link

k33g commented Dec 6, 2013

@sjmiles yes, something like that

var xmlhttp = new XMLHttpRequest(); 
xmlhttp.open("POST", "humans","/json-handler");
xmlhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xmlhttp.send(JSON.stringify({
    username="ebidel", password:"abc"
}));

I'm going to try your tips :)

@k33g
Copy link

k33g commented Dec 6, 2013

@sjmiles 👍 great, it works fine, thank you very much :-)

tessalt added a commit to tessalt/google-tasks-client that referenced this issue May 20, 2014
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants