Javascript \将双字节日语空格更改为英语空格

时间:2015-06-29 11:45:17

标签: javascript string internationalization

我有一个文本框来输入文本,但服务器无法处理双字节空间。 我希望在发送之前将其转换为常规空间。

试图做

var content = "よろしいでしょう キャンセル"
content = content.replace(" ", " ");

但现在IDE正在尖叫它无法以这种方式保存它。 有标准的方法吗?

来自Fiddler的请求标题:

POST /blablabla/SearchTerm HTTP/1.1
Host: localhost
Connection: keep-alive
Content-Length: 290
Accept: application/json
Origin: http://localhost
X-Requested-With: XMLHttpRequest
Accept-Language: en-US
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36
Content-Type: application/json
Referer: http://localhost/blablabla
Accept-Encoding: gzip, deflate

并且请求本身来自fiddler:

{...."content":"\u3088\u308d\u3057\u3044\u3067\u3057\u3087\u3046\u3000\u30ad\u30e3\u30f3\u30bb\u30eb"....}

和来自javascript的Ajax调用

  // Invoke request to server and register to the success and failure events
            Ext.Ajax.request({
                url: '/blablabla/SearchTerm',
                method: "POST",
                headers: this.header || {
                    "Content-Type": "application/json",
                    "Accept": "application/json"
                },
                jsonData: {                  
                    content: content,                   
                },
                timeout: this.configuration.getAjaxRequestTimeout(),
                success: searchCompletedSuccessDelegate,
                failure: searchCompletedFailureDelegate
            });

1 个答案:

答案 0 :(得分:0)

我猜你的IDE不支持space \ u3000的编码,所以你可以使用unuode符号和\ u escape(不知道真名):

//will work in any case
var content = "\u3088\u308d\u3057\u3044\u3067\u3057\u3087\u3046\u3000\u30ad\u30e3\u30f3\u30bb\u30eb";
content.replace("\u3000"," ")

我不了解VS,但有些IDE可以将文件配置为UTF-8以避免出现此问题。