Jquery:ajax 发布和编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/965150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 10:16:02  来源:igfitidea点击:

Jquery: ajax post and encoding

jquery

提问by Daniel

I am unable to understand why I can't get a correct ISO-8859-1 charstet from the server answer. Being this a work on legacy code, i hardly could change charset encoding on the pages.

我无法理解为什么我无法从服务器答案中获得正确的 ISO-8859-1 字符集。由于这是对遗留代码的工作,我几乎无法更改页面上的字符集编码。

I make use of the JQuery call

我使用 JQuery 调用

$.post("server-side-code", {t:ctext, i:ioff, sid:sessionid},  
    function(data, status) {            
       $('#chk').append(data); 
     });

posting a textarea value created using javascript:

发布使用 javascript 创建的 textarea 值:

<form accept-charset='ISO-8859-1' method='post'>
<textarea cols='40' rows='8' id='commento'></textarea><br>
<input type='button' value='invia' id='submit'></form>

The server side script processing the request declares at its very top:

处理请求的服务器端脚本在其最顶部声明​​:

text/html; charset=ISO-8859-1

so, honestly, I can't figure out what else I should declare, in terms of encoding. This notwithstanding, the accented characters "àèéìòù" bounce back as: "? ?¨?????2?1" when placing the server answer in an HTML element

所以,老实说,在编码方面,我不知道我还应该声明什么。尽管如此,当将服务器答案放在 HTML 元素中时,重音字符“àèéìòù”会反弹为:“????????2?1”

The source is saved as ascii. Tryng to do this to have rudimentary Html encoding on the variable to be posted does not solve:

源文件保存为 ascii。尝试这样做以对要发布的变量进行基本的 Html 编码并不能解决:

ctext = escapeHTML(ctext);

function escapeHTML (str)
{
   var div = document.createElement('div');
   var text = document.createTextNode(str);
   div.appendChild(text);
   return div.innerHTML;
}; 

Some idea?

有什么想法?

Thanks!

谢谢!

采纳答案by Rodrigo Asensio

I have a better solution now. Both post and get works PERFECTLY. I'm working over tomcat who by default handle ISO 8859 stuff.

我现在有一个更好的解决方案。发布和获取作品都完美无缺。我正在处理默认处理 ISO 8859 内容的 tomcat。

Web page properties:

网页属性:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

charset of the webpage inside the head.

头部内网页的字符集。

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

Now, all my parameteres are escaped with escape function, I provided this solution before.

现在,我所有的参数都用转义函数转义了,我之前提供了这个解决方案。

(function($) {$.fn.escape = function() {
    return escape(this.val());};})(jQuery);

Now, when sending the ajax request I set the contentType to this:

现在,在发送 ajax 请求时,我将 contentType 设置为:

contentType : "application/x-www-form-urlencoded; charset=iso-8859-1"

And finally , when receiving the parameters at the servlet or any receiver I get a decoded parameter using this.

最后,当在 servlet 或任何接收器处接收参数时,我使用它得到一个解码参数。

    public String getHttpParameter(String name) throws Exception {
    String value = this.getHttpRequest().getParameter(name);
    return value == null || value.isEmpty() ? value : URLDecoder.decode(value,"ISO-8859-1");
}

POST and GET works perfectly in IE 7 and 8 , SAFARI, CHROME and FIREFOX.

POST 和 GET 在 IE 7 和 8、SAFARI、CHROME 和 Firefox 中完美运行。

回答by Ciro

I have used the following in javascript

我在javascript中使用了以下内容

escape($("#some_id").val()) 

and the following in PHP

和以下 PHP

urldecode($var)

It worked for me.

它对我有用。

回答by Hardik Soni

Please try using escape() in javascript and urldecode() in php file

请尝试在 javascript 中使用 escape() 并在 php 文件中使用 urldecode()

it will work.

它会起作用。

回答by chaos

Not sure if it's what's breaking your situation, but accept-charsetis extremely poorly supported, and you may as well not even use it. It's the page encoding that will control what gets sent back to the server.

不确定它是否破坏了您的情况,但accept-charset支持极差,您甚至可能不使用它。页面编码将控制发送回服务器的内容。

It'd be useful if you looked at the saved files on your server to see whether the data in them is good or not. That'd at least establish whether the client->server part of the transaction is working.

如果您查看服务器上保存的文件以查看其中的数据是否良好,这将很有用。这至少可以确定事务的客户端-> 服务器部分是否正常工作。

回答by chaos

General information: http://www.w3.org/International/O-HTTP-charset

一般信息:http: //www.w3.org/International/O-HTTP-charset

try to convert the utf-8 string with utf8_decode() in php with the incomming and outgoing data.

尝试使用传入和传出数据在 php 中使用 utf8_decode() 转换 utf-8 字符串。

回答by Rodrigo Asensio

All my pages are ISO-8859-1, my response is also 8859 but jquery ajax was freaking me out. My solution was wrap the original $.ajax functionality with this. Works Perfectly for GET request, and I will post soon the solution for POST request.

我所有的页面都是 ISO-8859-1,我的响应也是 8859,但 jquery ajax 吓坏了我。我的解决方案是用这个包装原始的 $.ajax 功能。非常适合 GET 请求,我将很快发布 POST 请求的解决方案。

The call:

电话:

$.myrequest({
  url: "/myapp/myurl",
  params: {
    key1: $("#myinput").escape()
  }
});

the source for escape function

转义函数的来源

(function($) {
$.fn.escape = function() {
    return escape(this.val());
};})(jQuery);

the source for myrequest function

myrequest 函数的来源

(function($) {

$.request = function(settings) {

    /**
     * generates a string that will be passed as 
     * data instead an object, but we have the capability
     * of pass an object but in another variable called param
     */
    function _parseParameters(settings) {
        var data = "";
        for (var param in settings.params) {
            data += param + "=" + settings.params[param] + "&";
        }
        settings.data = data;
    }
    _parseParameters(settings);
    $.ajax(settings);
}})(jQuery);

回答by kgiannakakis

As far as I know jQuery uses the encoding of the web page that invokes the AJAX method to interpret the received data. Are you using the correct encoding for the page? If yes, then most probably the error lies with the server code. Try calling the AJAX handler programmatically (outside the Web Browser) to see what it is returning.

据我所知,jQuery 使用调用 AJAX 方法的网页编码来解释接收到的数据。您是否为页面使用了正确的编码?如果是,那么错误很可能出在服务器代码上。尝试以编程方式(在 Web 浏览器之外)调用 AJAX 处理程序以查看它返回的内容。

回答by Nux

I was searching high and low for a similar problem (Windows-1250 request with AJAX). I've found a solution and posted here: How can be request variables encoded by encodeURIComponent with ISO-8859-1 charset and not utf-8?

我正在寻找类似问题的高低(Windows-1250 请求与 AJAX)。我找到了一个解决方案并在这里发布: 如何使用 ISO-8859-1 字符集而不是 utf-8 编码由 encodeURIComponent 编码的请求变量?

回答by Josue

Maybe you have forgot to declare the contentType on the server side before to specify it as UTF-8, in this case, the contentType is null and the server don′t know what kind of data it′s dealing with. Once you declare the contentType as UTF-8, the server is capable to understand which kind of data is working with. Declaring the contentType as UTF-8 is not the correct solution, although it helps. The answer provided by Rodrigo probably would be the best solution for your problem.

也许你之前忘记在服务器端声明 contentType 指定它为 UTF-8,在这种情况下,contentType 为 null 并且服务器不知道它正在处理什么样的数据。一旦您将 contentType 声明为 UTF-8,服务器就能够了解正在处理的数据类型。将 contentType 声明为 UTF-8 不是正确的解决方案,尽管它有帮助。Rodrigo 提供的答案可能是您问题的最佳解决方案。