强制"charset = x-用户定义"在jQuery Ajax Post上 [英] Force "charset=x-user-defined'" on jQuery Ajax Post

查看:194
本文介绍了强制"charset = x-用户定义"在jQuery Ajax Post上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Javascript应用程序调用Hessian Web服务,但是解析响应时遇到问题,因为jQuery将响应视为文本并剥离了它的第一个字节. 在我的研究中,我发现您需要将字符集设置为'charset = x-user-defined',以便浏览器保持原样.但是,根据ajax文档:

I am trying to call a Hessian web service from a Javascript application, but I'm having issues parsing the response, since jQuery is treating the response as text and stripping the first bytes of it. In my research, I have found out that you need to set the charset as 'charset=x-user-defined' in order to the browser leave my bytes as is. But, according the ajax docs:

将数据发送到服务器

默认情况下,使用GET HTTP方法发送Ajax请求.如果 POST方法是必需的,可以通过设置一个方法来指定 类型选项的值.此选项会影响 数据选项发送到服务器. POST数据将始终为 根据W3C使用UTF-8字符集传输到服务器 XMLHTTPRequest标准.

By default, Ajax requests are sent using the GET HTTP method. If the POST method is required, the method can be specified by setting a value for the type option. This option affects how the contents of the data option are sent to the server. POST data will always be transmitted to the server using UTF-8 charset, per the W3C XMLHTTPRequest standard.

事实上,无论我使用什么设置,字符集都不会改变.我已经尝试了以下各项,一次又一次,没有运气

And indeed, the charset is not changing regardless of the settings I used. I have tried the following, separately and all at once, with no luck

$.ajax({
        type : 'POST',
        url : url,
        timeout : 3000,
        data : parameters,

        contentType : "x-application/hessian; charset=x-user-defined'",
        mimeType: 'text/plain; charset=x-user-defined',
        headers: { 
            Accept : "text/plain; charset=x-user-defined",
            "Content-Type": "text/plain; charset=x-user-defined"
        },
        beforeSend : function(xhr) {                
            xhr.overrideMimeType("text/plain; charset=x-user-defined");            
        }
    })

我还试图弄乱jQuery中定义的数据转换器和自定义内容类型,但没有成功.

Also I tried to mess around with the data converters and custom contenttypes defined in jQuery, with no succes.

看来,按照标准,我将无法执行此操作.它适用于GET,但不适用于POST,并且Hessian协议需要POST.

It appears that as per the standard, I will not be able to do this. It works with GET but not with POST, and the Hessian protocol requires POST.

您有什么想法吗?还是我需要从头开始构建我的XHR方法?

Do you have any ideas? Or do I need to start to build my XHR method form scratch?

推荐答案

原来我在其他地方犯了一个愚蠢的错误.但是无论如何,我找到了一种处理请求和响应中的二进制数据的好方法,

Turns out that I was making a silly mistake somewhere else. But anyhow, I found a sweet way for handling binary data on request and responses, from here.

define(function() {
// Do setup work here
function configurationException(message) {
    throw new Error(message + " missing from configuration object");
}

return {
    post : function(config) {
        if (config) {
            var url = config.url || configurationException("url");
            var done = config.done || configurationException("callback function");
            var timeout = config.timeout || 10000;
            var data;
            if (config.data) {
                data = config.data;
            } else {
                data = null;
                console.warn('No data is specified in binaryPost');
            }

            var request = new XMLHttpRequest();
            request.open("POST", url, true);
            request.responseType = "arraybuffer";
            request.setRequestHeader("Content-Type", "x-application/hessian;");

            request.onload = function(oEvent) {
                var arrayBuffer = request.response; // Note: not oReq.responseText
                if (arrayBuffer) {
                    var byteArray = new Uint8Array(arrayBuffer);

                    done(byteArray);
                }
            };

            request.send(data);

        } else {
            throw new Error("Configuration object is missing");
        }
    }
};
});

希望您发现它有用

这篇关于强制"charset = x-用户定义"在jQuery Ajax Post上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆