How to fix the "charset" of an XML document?

Asked

Viewed 107 times

1

I have that code

$.ajax({
    url: "https://crossorigin.me/http://www.acidigital.com/rss/rss_santo.php",
    crossDomain: true,
    contentType: "application/xml; charset:utf-8",
    type: "GET",
    dataType: "xml",
    success: function (re) {

        console.log(re) // documento XML puro

    }
})

It turns out that the XML document of this request (http://www.acidigital.com/rss/rss_santo.php) comes with special characters replaced by "".

  • 1

    In the XML source you see the ISO-8859-1 declaration in the first line: <?xml version="1.0" encoding="ISO-8859-1"? > And you’re treating as utf-8 in your code. Working that would be the starting point.

  • @Bacco just want to replace the correct characters. In fact all accented letters look like this.

  • Almost Bob, and use contentType: "application/xml; charset:ISO-8859-1" and not UTF-8

  • @Kingrider tried, the same.

  • It must be your php has some wrong charset and puts the returno utf8_encode("texto texto texto);

  • @Kingrider PHP is not mine, only consumption, when I took this XML by another AJAX engine with proxy came without this problem.

  • @ropbla9 understood, and I’ll think how to convert for you =S

  • @ropbla9 I found about conversion is overrideMimeType('text/html;charset=iso-8859-1') and it’s not for jquery and pure has (Xmlhttprequest) to work, and I haven’t found jquery solution for mime. I already closed the code ready under this done. see you.

Show 3 more comments

1 answer

1


Follow the example code:

$.ajax({
    url: "https://crossorigin.me/http://www.acidigital.com/rss/rss_santo.php",
    crossDomain: true,
    contentType: "application/xml; charset:ISO-8859-1",
    type: "GET",
    beforeSend: function(charset) {
        charset.overrideMimeType('text/html; charset=iso-8859-1');
    },
    success: function (re) {
      xmltemp = $.parseXML(re);
      $texto = $(xmltemp).find('description');
      console.log($texto.text());
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<pre></pre>

Read about Xmlhttprequest has conversion/manipulation:

https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Usando_XMLHttpRequest

see you around

  • Perfect, man! @Kingrider You can explain?

  • An ajax object property (http://i.imgur.com/W4h7abn.png) ... found has mime as overrideMimeType, This mime is a formatting. Read more http://canaltech.com.br/o-que-e/o-que-e/O-que-e-MIME/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.