简体   繁体   中英

iron-ajax trying to transmit by xhr multiline string or array subexpression

I have a multiline string coming from a paper-textarea element that I am trying to transmit to the server.

My first attempt was to send it just like that to the server, but iron-ajax cuts out the newline characters, presumably because of json encoding issues.

My second attempt involves splitting the string lines into entries of an array, so let's see how that goes.

<iron-ajax 
    ...
    params={{ajax_new_tag_and_entry}}
    ...
</iron-ajax>

This is the function that changes 'ajax_new_tag_and_entry":

tap_submit_entry : function(){
        this.ajax_new_tag_and_entry=
            { tag : this.journal_tags[this.the_journal_tag].tag,
                entry : this.the_journal_entry.split("\n") };
        console.log(this.the_journal_entry);
        console.log(this.the_journal_entry.split("\n"));
    }

When I do 'console.log(this.the_journal_entry);' I get:

One message
to rule
them all.

When I do 'console.log(this.the_journal_entry.split("\\n"));' I get:

Array [ "One message", "to rule", "them all." ]

But the Firefox developer tools tell me that this are the parameters sent to the server:

tag:"general_message"
entry:"One message"
entry:"to rule"
entry:"them all."

This obviously means that entry was split up into three identical entries for the xhr parameters, instead of being one single entry array with the three lines in the message.

I would appreciate it if anyone has any thoughts on how I could fix this issue.

If newlines are to be preserved, the server (or the data receiver) ultimately needs to recover the newlines using an agreed upon format.

Splitting the multi-line string into an array (as you've done) is one way of doing it. The server/receiver would then join the array with newlines as deserialization. Note that it's perfectly acceptable to have multiple query parameters of the same name in a URL, which the receiver could merge into an array (as seen in this Flask test app ).

Alternatively, you could encode (ie, replace \\n with %0A ) or escape (ie, replace \\n with \\\\n ) the newlines. Then, the server/receiver has to decode/unescape them to restore the original message.

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