热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何确保使用jquery$.post在UTF8中提交的变量-HowtoensurevariablessubmittedinUTF8usingjquery$.post

Ihavebeenstrugglingwiththisforthreedaysnowandthisiswhatihavegotandicannotunderst

I have been struggling with this for three days now and this is what i have got and i cannot understand why i am seeing this behavior.

我已经挣扎了三天了,这就是我所得到的,我不明白为什么我看到这种行为。

my problem is that i have a MySql spanish db with char set and collation defined as utf8_general_ci. when i query the data base in delete.php like this "DELETE FROM countryNames WHERE country = '$name'"

我的问题是,我有一个带有char设置和排序的MySql西班牙db,定义为utf8_general_ci。当我在delete中查询数据库时。php类似于"从国家名中删除,其中国家= '$name'"

the specified row doesnot get deleted. i am setting the variable $name in delete.php through a post variable $name=$_post['data'] . mostly $name gets the value in spanish characters e.g español, México etc. the delete.php file gets called from main.php.if i send a post message from main.php $.post("delete.php", {data:filename}); , the query doesnot deletes the entry (although the 'filename' string is in utf8) but if i create a form and then post my data variable in main.php, the query works!! the big question to me is why do i have to submit a form for the query to work? what im seeing is my database rejects the value if it comes from a jquery post call but accepts it when its from a submitted form. (i make no code change for the query to work. just post the value by submiting the form)

指定的行不会被删除。我在delete中设置变量$name。php通过post变量$name=$_post['data']。$name通常用西班牙语字符e表示。西班牙,墨西哥等国家删除。从main.php调用php文件。如果我从main发送邮件。php $ . post(“删除。php”,{数据:文件名});,查询不会删除条目(尽管“文件名”字符串在utf8中),但是如果我创建一个表单,然后将数据变量以main的形式发布的话。php,查询工作! !对我来说,最大的问题是为什么我必须提交一个表单才能进行查询?我看到的是我的数据库拒绝来自jquery post调用的值,但接受来自提交表单的值。(对于查询,我不做任何代码更改。只要提交表单就可以了)

4 个解决方案

#1


2  

First of all, to see what charset ìs used for requests, install something like Firebug and check the 'Content-Type' header of your request/response. It will look something like 'application/json; charset=...'. This should be charset=utf-8 in your case.

首先,要查看请求使用的字符集,请安装Firebug之类的东西,并检查请求/响应的“Content-Type”头。它看起来像'application/json;charset =…”。在您的例子中,应该是charset=utf-8。

My guess why it worked when posting a form is probably because of x-www-form-urlencoded - non-alphanumeric characters are additionally encoded on the client side and again decoded on the server, that's the difference to posting the data directly.

我猜测,在发布表单时,它为什么会起作用,可能是因为x-www-form- urlencodes—非字母数字字符在客户端被额外编码,然后在服务器上再次解码,这与直接发布数据是不同的。

This means that somewhere there is a wrong encoding at work. PHP treats your strings agnostic to its encoding by default, so I would tend to rule it out as the source of the error. jQuery.post also uses UTF-8 by default... so my suspect is the filename variable. Are you sure it is in UTF-8? Where and how do you retrieve it?

这意味着在某些地方存在错误的编码。PHP默认情况下不考虑字符串的编码,因此我倾向于将其排除为错误的来源。jQuery。post默认也使用UTF-8…我的怀疑对象是文件名变量。你确定是UTF-8吗?你在哪里以及如何取回它?

You should probably also ensure that the actual HTML page is also sent as UTF-8 and not, let's say iso-8859-1. Have a look at this article for a thorough explanation on how to get it right.

您还应该确保实际的HTML页面也被发送为UTF-8,而不是,比方说iso-8859-1。看一看这篇文章,就如何正确地做一个详细的解释。

#2


1  

guys this was a Mac problem!! i just tested it on windows as my server and now everything works fine. So beware when u r using Mac as a server with MySql having UTF8 as charset and collation. I guess the Mac stores the folder and file name in some different encoding and not UTF-8.

伙计们,这是Mac的问题!!我刚刚在windows上测试了它作为我的服务器,现在一切正常。所以,当你使用Mac作为一个服务器时,要小心,因为MySql的UTF8是charset和collation。我猜Mac将文件夹和文件名存储在不同的编码中,而不是UTF-8。

#3


0  

You answer might be here: How to set encoding in .getJSON JQuery

您的答案可能是:如何在.getJSON JQuery中设置编码

As it says there, use $.ajax instead of $.post and you can set encoding.

正如上面所说,使用$。ajax而不是美元。post和您可以设置编码。

OR, as it says in the 2nd answer use $.ajaxSetup to set the encoding accordingly.

或者,就像第二种回答说的,用$。ajaxSetup相应地设置编码。

#4


0  

Use .serialize() ! I think it will work. More info: http://api.jquery.com/serialize/

使用.serialize()!我想这行得通。更多信息:http://api.jquery.com/serialize/。


推荐阅读
  • 搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的详细步骤
    本文详细介绍了搭建Windows Server 2012 R2 IIS8.5+PHP(FastCGI)+MySQL环境的步骤,包括环境说明、相关软件下载的地址以及所需的插件下载地址。 ... [详细]
  • 本文介绍了Web学习历程记录中关于Tomcat的基本概念和配置。首先解释了Web静态Web资源和动态Web资源的概念,以及C/S架构和B/S架构的区别。然后介绍了常见的Web服务器,包括Weblogic、WebSphere和Tomcat。接着详细讲解了Tomcat的虚拟主机、web应用和虚拟路径映射的概念和配置过程。最后简要介绍了http协议的作用。本文内容详实,适合初学者了解Tomcat的基础知识。 ... [详细]
  • 本文介绍了通过ABAP开发往外网发邮件的需求,并提供了配置和代码整理的资料。其中包括了配置SAP邮件服务器的步骤和ABAP写发送邮件代码的过程。通过RZ10配置参数和icm/server_port_1的设定,可以实现向Sap User和外部邮件发送邮件的功能。希望对需要的开发人员有帮助。摘要长度:184字。 ... [详细]
  • 如何实现织梦DedeCms全站伪静态
    本文介绍了如何通过修改织梦DedeCms源代码来实现全站伪静态,以提高管理和SEO效果。全站伪静态可以避免重复URL的问题,同时通过使用mod_rewrite伪静态模块和.htaccess正则表达式,可以更好地适应搜索引擎的需求。文章还提到了一些相关的技术和工具,如Ubuntu、qt编程、tomcat端口、爬虫、php request根目录等。 ... [详细]
  • 本文详细介绍了SQL日志收缩的方法,包括截断日志和删除不需要的旧日志记录。通过备份日志和使用DBCC SHRINKFILE命令可以实现日志的收缩。同时,还介绍了截断日志的原理和注意事项,包括不能截断事务日志的活动部分和MinLSN的确定方法。通过本文的方法,可以有效减小逻辑日志的大小,提高数据库的性能。 ... [详细]
  • Spring源码解密之默认标签的解析方式分析
    本文分析了Spring源码解密中默认标签的解析方式。通过对命名空间的判断,区分默认命名空间和自定义命名空间,并采用不同的解析方式。其中,bean标签的解析最为复杂和重要。 ... [详细]
  • Linux重启网络命令实例及关机和重启示例教程
    本文介绍了Linux系统中重启网络命令的实例,以及使用不同方式关机和重启系统的示例教程。包括使用图形界面和控制台访问系统的方法,以及使用shutdown命令进行系统关机和重启的句法和用法。 ... [详细]
  • http:my.oschina.netleejun2005blog136820刚看到群里又有同学在说HTTP协议下的Get请求参数长度是有大小限制的,最大不能超过XX ... [详细]
  • 关键词:Golang, Cookie, 跟踪位置, net/http/cookiejar, package main, golang.org/x/net/publicsuffix, io/ioutil, log, net/http, net/http/cookiejar ... [详细]
  • Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOMEbinjava–option来启 ... [详细]
  • 本文介绍了在Linux下安装和配置Kafka的方法,包括安装JDK、下载和解压Kafka、配置Kafka的参数,以及配置Kafka的日志目录、服务器IP和日志存放路径等。同时还提供了单机配置部署的方法和zookeeper地址和端口的配置。通过实操成功的案例,帮助读者快速完成Kafka的安装和配置。 ... [详细]
  • Windows7 64位系统安装PLSQL Developer的步骤和注意事项
    本文介绍了在Windows7 64位系统上安装PLSQL Developer的步骤和注意事项。首先下载并安装PLSQL Developer,注意不要安装在默认目录下。然后下载Windows 32位的oracle instant client,并解压到指定路径。最后,按照自己的喜好对解压后的文件进行命名和压缩。 ... [详细]
  • 解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法
    本文介绍了解决nginx启动报错epoll_wait() reported that client prematurely closed connection的方法,包括检查location配置是否正确、pass_proxy是否需要加“/”等。同时,还介绍了修改nginx的error.log日志级别为debug,以便查看详细日志信息。 ... [详细]
  • android listview OnItemClickListener失效原因
    最近在做listview时发现OnItemClickListener失效的问题,经过查找发现是因为button的原因。不仅listitem中存在button会影响OnItemClickListener事件的失效,还会导致单击后listview每个item的背景改变,使得item中的所有有关焦点的事件都失效。本文给出了一个范例来说明这种情况,并提供了解决方法。 ... [详细]
  • HDFS2.x新特性
    一、集群间数据拷贝scp实现两个远程主机之间的文件复制scp-rhello.txtroothadoop103:useratguiguhello.txt推pushscp-rr ... [详细]
author-avatar
ecrbw_9870105634
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有