How to change ajax-charset?

2019-01-11 19:26发布

How can I change the default encoding used by $.post()?

The arguments are encoded with UTF-8. How can I encode it with ISO 8859-1?

3条回答
你好瞎i
2楼-- · 2019-01-11 19:36

You could use:

contentType:"application/x-javascript; charset:ISO-8859-1"
查看更多
Melony?
3楼-- · 2019-01-11 19:36

It seems the charset cannot be changed anymore – $.ajax docs states:

The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding.

查看更多
我想做一个坏孩纸
4楼-- · 2019-01-11 19:47

By giving the content type explicitly during ajax call as below may allow you to override the default content type.

$.ajax({
        data: parameters,
        type: "POST",
        url: ajax_url,
        timeout: 20000,
        contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
        dataType: 'json',
        success: callback
});

You would also have to specify the charset on the server.

Ex: for php

<?php header('Content-Type: text/html; charset=ISO-8859-15'); ?>

I hope this may help you.

查看更多
登录 后发表回答