简体   繁体   English

Iron-ajax尝试通过xhr多行字符串或数组子表达式进行传输

[英]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. 我有一个来自paper-textarea元素的多行字符串,我正尝试将其传输到服务器。

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. 我的第一个尝试是将它像这样发送到服务器,但是iron-ajax剪切了换行符,可能是因为json编码问题。

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": 这是更改“ 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);' 当我做'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"));' 当我做'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: 但是Firefox开发人员工具告诉我,这是发送到服务器的参数:

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. 显然,这意味着该条目针对xhr参数分为三个相同的条目,而不是消息中包含三行的单个条目数组。

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 ). 请注意,在URL中具有多个相同名称的查询参数是完全可以接受的,接收者可以将其合并到一个数组中(如在此Flask测试应用中所示 )。

Alternatively, you could encode (ie, replace \\n with %0A ) or escape (ie, replace \\n with \\\\n ) the newlines. 或者,您可以对换行符进行编码(即用%0A替换\\n )或换行(即用\\\\n替换\\n \\\\n )。 Then, the server/receiver has to decode/unescape them to restore the original message. 然后,服务器/接收器必须对其进行解码/转义以恢复原始消息。

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

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