问题描述
我在使用laravel和JS(jquery)构建的应用程序中遇到字符串截断问题。起初我认为这是一个后端问题(我问了这个问题:)。仔细调试之后,我注意到我的JS代码中出现了问题。
I have problems with string truncation in my app built with laravel and JS(jquery). At first I thought it was a back end problem (and I asked this question:Laravel Truncating Strings). After careful debugging I noticed there's something wrong in my JS code.
我试图通过JQuery post(AJAX)请求将一些字符串从CKEditor传递到后端,但是在某些情况下,字符串被截断,特别是包含特殊字符的嵌入!
I am trying to pass some string from CKEditor via a JQuery post(AJAX) request to the backend, However in some cases the string is truncated especially with embeds that contains special characters!
以下是出现问题的代码段
var content = CKEDITOR.instances.editor.getData();
alert("Content :" + content + "<br> Length :" + content.length);
var data = 'post_id=' + post_id + '&content=' + content + '&_token=' + $('#token').val();
alert("Data :" + data);
alert("Content Again :" + content);
发生了什么事?
- 我从CKEditor获取字符串
- 提醒内容和调试长度,看起来都不错
- 我将内容连接到数据变量,后来在ajax
请求中发送。 - 警报数据显示在某些情况下截断的字符串
- I再检查内容一切都很好,但由于某种原因,数据是
截断。
- I get string from CKEditor
- Alert the content and the length for debugging and all looks good
- I concatenate content to the data variable which is later sent in an ajaxrequest
- alert data shows truncated string in certain cases
- I check content again its all good but for some reason data was truncated.
示例???
我将这个facebook嵌入编辑器中
I paste this facebook embed in the editor
<iframe src="https://www.facebook.com/plugins/comment_embed.php?href=https%3A%2F%2Fwww.facebook.com%2Ftonyelumelu%2Fposts%2F10154690113371949%3Fcomment_id%3D10154690136096949&include_parent=false" width="560" height="161" style="border:none;overflow:hidden" scrolling="no" frameborder="0" allowTransparency="true"></iframe>
警告内容显示正确的结果而不截断。 但警告数据变量给出
Alerting the content shows the correct result without truncating. But alerting the data variable gives
Data :post_id='+post_id+'&content=<p><iframe src="https://www.facebook.com/plugins/comment_embed.php?href=https%3A%2F
显然字符串被削减%2F
有时%3A
(另一个嵌入我尝试过)
clearly the string is being cut around %2F
sometimes %3A
(another embed I tried)
数据变量看起来如何使用普通字符串(< p>这是一些字符串< / p>
)
How data variable looks with normal string (<p> This is some string </p>
)
Data :post_id='+post_id+'&content=<p>This is some string</p>
&_token='+$('#token').val()
请问,我做错了什么?一般如何避免JS中的这种截断我看到字符串在不同的情况下被截断几次很难解释。提前谢谢
Please, what am I doing wrong? and generally how can I avoid such truncation in JS I have seen strings truncated several times in different situations that really hard to explain. Thanks in advance
推荐答案
如果内容
有特殊字符,让我们说它是一些像你和&我永远
,那么你的数据
变量将如下所示:
If content
has especial characters, let's say it's something like You & I forever
, then your data
variable would look like this:
post_id=1&content=You & I forever&_token=bla
看到问题?内容在 You
之后结束,因为&符号未编码。所以你应该做这样的事情:
See the problem? Content is ending right after You
, because the ampersand isn't encoded. So you should do something like this:
var data = 'post_id=' + post_id + '&content=' + encodeURIComponent(content) + '&_token=' + $('#token').val();
这样,您的数据
变量就会显示像这样:
With that, your data
variable would look like this:
post_id=1&content=You%20%26%20I%20forever&_token=bla
这一切都能顺利进行
这篇关于Javascript在连接期间截断字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!