简体   繁体   English

javascript - 发送没有字符串连接的ajax请求

[英]javascript - send ajax request without string concatenation

I have a page in wich the user will write a code (in a programming language).我有一个页面,用户将在其中编写代码(使用编程语言)。 the code can be very long (more than 2000 characters) and can contain any type of characters.代码可以很长(超过 2000 个字符)并且可以包含任何类型的字符。 And when he press the send button the code will be sent (using javascript ajax) to a script file on the server etc..当他按下发送按钮时,代码将被发送(使用 javascript ajax)到服务器上的脚本文件等。

currently i am using this to send the code with ajax :目前我正在使用它来发送带有 ajax 的代码:

jxobj.open('POST', 'www.my-website.com/my_script.php', true);
jxobj.send('code=' + code);

but if the code contains some characters like & then the value of code will be broken into many pieces and i can't retrieve it in my script file.但是如果代码包含一些像&这样的字符&那么code的值将被分解成许多部分,我无法在我的脚本文件中检索它。

Is there any way to send ajax request without a string concatenation?有没有办法在没有字符串连接的情况下发送ajax请求? I would like to use only javascript if possible and thanks.如果可能,我只想使用 javascript,谢谢。

The data format is well documented and the standard JavaScript encodeURIComponent function will encode data for it.数据格式有据可查,标准的 JavaScript encodeURIComponent函数将为它编码数据。

jxobj.send('code=' + encodeURIComponent(code));

Is there any way to send ajax request without a string concatenation?有没有办法在没有字符串连接的情况下发送ajax请求?

Not really.并不真地。 The send method takes a string, so you have to build a string using some mechanism or another. send 方法接受一个字符串,因此您必须使用某种机制构建一个字符串。 You could achieve it with (for instance) arrays and joins, but it still comes down to the same basic principles.您可以使用(例如)数组和连接来实现它,但它仍然归结为相同的基本原则。

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

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