I have a real simple form with a textbox and a button, and my goal is to have an asynchronous request (jQuery: $.ajax) send the text to the server (PHP/mysql a la Joomla) so that it can be added to a database table.

我有一个带有文本框和按钮的简单表单,我的目标是让一个异步请求(jQuery:$ .ajax)将文本发送到服务器(PHP / mysql a la Joomla),以便可以添加到数据库表。

Here's the javascript that is sending the data from the client:

这是从客户端发送数据的javascript:

var value= $('#myvalue').val();

$.ajax( {
    type:       "POST",
    url:        "/administrator/index.php",
    data:       {   option: "com_mycomponent",
                    task:   "enterValue",
                    thevalue: value,
                    format: "raw"},
    dataType:   "text",
    success:    reportSavedValue
} );

The problem arises when the user enters text with a space in it. The $_POST variable that I receive has all the spaces stripped out, so that if the user enters "This string has spaces", the server gets the value "Thisstringhasspaces".

当用户输入带有空格的文本时,就会出现问题。我收到的$ _POST变量删除了所有空格,因此如果用户输入“This string has spaces”,服务器将获得值“Thisstringhasspaces”。

I have been googling around, and have found lots of references that I need to use encodeURIComponent. So I have tried it, but now the value that I get from $_POST is "This20string20has20spaces".

我一直在谷歌上搜索,并找到了许多我需要使用encodeURIComponent的参考资料。所以我试过了,但现在我从$ _POST获得的价值是“This20string20has20spaces”。

So it appears to be encoding it the way I would expect, only to have the percent signs stripped instead of the spaces, and leaving the hex numbers.

因此它似乎按照我期望的方式对其进行编码,只是将百分号符号剥离而不是空格,并留下十六进制数字。

I'm really confused. It seems that this sort of question is asked and answered everywhere on the web, and everywhere encodeURIComponent is hailed as the silver bullet. But apparently I'm fighting a different breed of lycanthrope. Does anyone have any suggestions?

我真的很困惑。似乎这种问题在网络上随处可见并得到解答,并且到处都是encodeURIComponent被誉为银弹。但显然我正在与不同种类的狼人作斗争。有没有人有什么建议?

2 个解决方案

#1


1

It turns out that there was extra filtering I didn't realize I was performing. Because this was all running through Joomla, I was using JRequest::getCmd('thevalue') instead of $_POST['theValue]. This function, it turns out, filters out all those troublesome characters, like '%'.

事实证明,我没有意识到我正在执行额外的过滤。因为这一切都是通过Joomla运行的,所以我使用的是JRequest :: getCmd('thevalue')而不是$ _POST ['theValue]。事实证明,这个函数可以过滤掉所有那些麻烦的字符,比如'%'。

So the final solution is to use encodeURIComponent on the client, as is unanimously suggested on the web:

因此,最终的解决方案是在客户端上使用encodeURIComponent,正如在Web上一致建议的那样:

var value = encodeURIComponent($('#myvalue').val());

And on the server, to trade getCmd() for getVar() which allows more control over the filtering, in combination with urldecode():

并且在服务器上,为getVar()交换getCmd(),它允许更多地控制过滤,结合urldecode():

$value = urldecode(JRequest::getvar('thevalue', JREQUEST_ALLOWHTML));

Again, big thanks to Karim79 and Cesar. I dub thee Harker and Helsing -- my heroes for the day! :)

再次,非常感谢Karim79和Cesar。我称你为Harker和Helsing - 我的英雄们! :)

更多相关文章

  1. PHP用空格分割文本为数组的方法
  2. 在Javascript中将带有空格的字符串数组转换为小写,然后在Webkit中
  3. javascript encodeURIComponent并将空格转换成+符号
  4. python1.返回一个字符串中出现次数第二多的单词 2.字符串中可能
  5. 计算所有字符,包括linux中的空格
  6. postgresql 数组 多了引号 空格处理
  7. sql2005指定字段插入空格。
  8. android textview空格占位符以及一些其他占位符汇总
  9. 关于Android资源文件中出现百分号的问题

随机推荐

  1. android中的spinner动态加载内容
  2. android8.0屏蔽状态栏下拉
  3. Qt for Android Flutter - AAPT: error:
  4. Android 创建悬停通知栏
  5. android之hardwareAccelerated你不知道的
  6. android 横向ListView 横向列表
  7. china
  8. 10 Must-Have Android Tools for Develop
  9. Android各版本的版本号、版本名、API及发
  10. Android Firewall(防火墙) AndFire 首发