I am trying to get a response xml which has special characters in it.
This is failing in IE but in Mozilla it is working fine.
Pls let me know how to fix this.
Here's the code:
request.setCharacterEncoding("UTF-8");
response.setContentType("text/xml; charset=UTF-8");
response.setHeader("Cache-Control", "no-cache");
response.getWriter().write("<xml><valid><![CDATA[2189971_Bright Starts bath time foam ©®!#& toys each]]></valid><productid>123</productid></xml>");
Try adding the encoding in the XML itself:
response.getWriter().write("<?xml version=\"1.0\" encoding=\"UTF-8\"?><root><valid><![CDATA[2189971_Bright Starts bath time foam ©®!#& toys each]]></valid><productid>123</productid></root>");
Most likely your XML is invalid - you are specifying UTF8 encoding in the XML but writing code probably does not output UTF8. Check out what browser receives with some HTTP watcher (likle Fiddler) to make sure response is properly UTF8 encoded (the characters you are having problem with must be encode as their are above ASCII range).
Not sure what language/framework you are using, but setting encoding on request and writing to response looks suspicious.
Related
I have create one JSON file through PowerShell and place it on serve.
When i access that JOSN file through $.getJSON it works fine in crome and IE browser but when i access that JSON file in Firefox i got error of
JSON.parse: unexpected character at line 1 column 1 of the JSON data
Header:
Response:
What should be issue and how to fix it in firefox?
You've said that the server sends that JSON back with Content-Type: text/plain. The data appears to be in UTF-16 (probably, that's based on the screenshot), but the default charset for text/plain is us-ascii (see §4.1.2 of RFC2046):
4.1.2. Charset Parameter
A critical parameter that may be specified in the Content-Type field
for "text/plain" data is the character set. This is specified with a
"charset" parameter, as in:
Content-type: text/plain; charset=iso-8859-1
Unlike some other parameter values, the values of the charset
parameter are NOT case sensitive. The default character set, which
must be assumed in the absence of a charset parameter, is US-ASCII.
So, you need to change the response from the server such that it correctly identifies the character set being used, e.g. Content-Type: text/plain; charset=UTF-16 (obviously ensuring first that that is, in fact, the charset of the resource).
I'll just note that, from what I can make out of the JSON, it looks like it's primarily in a western script. If so, UTF-16 is unusual and inefficient choice, you'd probably be better off with UTF-8. But I only have a small fragment of the text to work from.
I am working on a Javascript project that uses AngularJS. When I get data with http request, all characters are appearing well. For example, a downloaded string with ajax is "räksmörgås", when written to the console as plain text, is appearing with ugly charecters.
console.log("räksmörgås") results into this: r�ksm�rg�s
Is this a file type encoding problem? Or are JavaScript strings always UTF-16 causing this problem?
I think the problem is that you are not using the correct charset. For Swedish try to change the character encoding to iso-8859-1 or windows-1252. I suppose that you are sending the server response without the correct headers and the browser interprets it as UTF-8 as the default charset.
So maybe changing the header charset as below will resolv the issue:
Content-Type: text/plain; charset=windows-1252 // or
Content-Type: text/plain; charset=iso-8859-1
Another solution would be to declare your script tag with charset, this way forcing Js to handle the characters to be interpreted with a specific encoding.
<script src="yourscritp.js" charset="UTF-8"></script>
Sending request with URL length ~ 4950 characters.
Getting the following XMLHTTPRequest.ResponseText:
ERROR
The requested URL could not be retrieved
While trying to retrieve the URL: ##my long url##
The following error was encountered:
Invalid URL
Some aspect of the requested URL is incorrect. Possible problems:
Missing or incorrect access protocol (should be `http://'' or similar)
Missing hostname
Illegal double-escape in the URL-Path
Illegal character in hostname; underscores are not allowed
Your cache administrator is webmaster.
But when I'm entering the same url in the browser it works just fine. I checked for possible errors(that are listed in the response text) - everything's ok.
When the number of parameters is less than ~200 the script works, so the clue must be in some limits. On the other hand there are no any settings in the apache or php or js.
Any advices or where should I look(some additional configs or whatever) for the solution?
Sending request with URL length ~ 4950 characters.
That is too much for Internet Explorer anyway. Also possibly for Opera, which IIRC has a limit of 4096 bytes for GET requests.
You should use POST for this amount of data.
Maximum URL length is 2,083 characters in Internet Explorer
Apache replies with 413 Entity Too Large if the URL exceeds approximately 4000 characters (request lines are capped to 8190 bytes).
Using the LimitRequestLine directive won't help, you'll have to recompile Apache with -D DEFAULT_LIMIT_REQUEST_LINE=some huge value if you absolutely want to send large GET requests.
EDIT: Some thoughts about the ~4000 character cap: 8190 looks a lot like 8192 with two bytes reserved for the string terminator, so there's a good chance that Apache uses UCS-2 or similar to store request lines, since DEFAULT_LIMIT_REQUEST_LINE is expressed in bytes, not characters.
That would give a 4095 character cap per request line, i.e. a maximum URL length of 4079 characters (taking into account the initial GET and the final CR/LF pair), which would make sense.
I am sending UTF-8, japanese text, to my server.
It works in Firefox. My access.log and headers are:
/ajax/?q=%E6%BC%A2%E5%AD%97
Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
Content-Type application/x-www-form-urlencoded; charset=UTF-8
Howeer, in IE8, my access.log says:
/ajax/?q=??
For some reason, IE8 is turning my AJAX call into question marks. Why!? I added the scriptCharset and ContentType according to some tutorials, but still no luck.
And this is my code:
$.ajax({
method:"get",
url:"/ajax/",
scriptCharset: "utf-8" ,
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
data:"q="+query ...,
...
})
Try encoding the query parameter with encodeURIComponent()
data:"q="+encodeURIComponent( query )
as bobince very correctly noted in his comment, if you use the object notation to pass parameters to the ajax method it will handle the encoding itself..
so
data:{ q : query }
will make jQuery handle the encoding ..
I'we read this post hoping it would solve the problem I had came across and that had to do with utf8 conversions.
In my case it turned out that the server engine (node.js) calculating the Content-length of the data with the data considered to be raw and not utf8, thus two character extended chars in uft8 was calculated as if they where one char resulting in the server sending one character too little.
See what I did to solve it here: Not well formed Json when sending to CouchDB
I know this is an old post but I had this problem recently and I'd like to contribute just in case someone else has the same problem.
I'm using PHP but I'm sure there's an option on every serverside language. It was just a couple of things:
Make sure you're sending the right headers on your ajax response by adding header('Content-Type: text/html; charset=utf-8'); This must be your first line. If you have any errors saying that headers have been sent already or something like that is because somewhere in your code you are outputing an extra space or something before sending the header so check your code.
When you build your response in your server, make sure you convert all your chars to the correspondig HTML char using echo htmlentities($your-string, null, 'utf-8); Because even after telling IE that you are sending utf-8 data, it seems like IE forgets that or it doesn't simply assume anything so adding this to your code will ensure the right output.
Thanks all for your help.
Use encodeURIComponent() in javaScript. Here is the sample:
function doPost()
{
var URL = "http://localhost/check.php?yab=" + encodeURIComponent(document.getElementById("formSearch").childNodes[1].value);
xmlHttp.open("GET", URL);
xmlHttp.send();
};
Before I begin, I would like to highlight the structure of what I am working with.
There is a text file from which a specific text is taken. The file is encoded in utf-8
Perl takes the file and prints it into a page. Everything is displayed as it should be. Perl is set to use utf-8
The web page Perl generates has the following header <meta content="text/html;charset=utf-8" http-equiv="content-type"/>. Hence it is utf-8
After the first load, everything is loaded dynamically via jQuery/AJAX. By flipping through pages, it is possible to load the exact same text, only this time it is loaded by JavaScript. The Request has following header Content-Type: application/x-www-form-urlencoded; charset=UTF-8
The Perl handler which processes the AJAX Request on the Backend delivers contents in utf-8
The AJAX Handler calls up a function in our custom Framework. Before the Framework prints out the text, it is displayed correctly as "üöä". After being sent to the AJAX Handler, it reads "x{c3}\x{b6}\x{c3}\x{a4}\x{c3}\x{bc}" which is the utf-8 representation of "üöä".
After the AJAX Handler delivers its package to the client as JSON, the webpage prints the following: "öäü".
The JS and Perl files themselves are saved in utf-8 (default setting in Eclipse)
These are the symptoms. I tried everything Google told me and I still have the problem. Does anyone have a clue what it could be? If you need any specific code snippet, tell me so and I'll try to paste it.
Edit 1
The Response Header from the AJAX Handler
Date: Mon, 09 Nov 2009 11:40:27 GMT
Server: Apache/2.2.10 (Linux/SUSE)
Keep-Alive: timeout=15, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: text/html; charset="utf-8"
200 OK
Answer
With the help of you folks and this page, I was able to track down the problem. Seems like the problem was not the encoding by itself, but rather Perl encoding my variable $text twice as utf-8 (according to the site). The solution was as simple as adding Encode::decode_utf8().
I was searching in the completely wrong place to begin with. I thank you all who helped me search in the right place :)
#spreads some upvote love#
returns the following: &38;&65;&116;&105;&108;&100;&101;&59;&38;&112;&97;&114;&97;&59;...
That's:
öäü
Which says your AJAX handler is using an HTML-entity-encoding function for its output, that is assuming input from the ISO-8859-1 character set. You could use a character-reference encoder that knew about UTF-8 instead, but probably it will be easier just to encode the potentially-special characters <>&"' and no others.
The Request has following header Content-Type: application/x-www-form-urlencoded; charset=UTF-8
There is no such parameter as charset for the MIME type application/x-www-form-urlencoded. This will be ignored. Form-encoded strings are inherently byte-based; it is up to the application to decide what character set they are treated as (if any; maybe the application does just want bytes).
This isn't an answer so much as a suggestion for debugging. The first thing that springs to mind is to try sending HTML entities like Ӓ instead of utf-8 codes. To make Perl send these there is surely a module or you can just do
my $text =~ s/(.)/"&#" . ord ($1) . ";"/ge;
The thing which it seems to me the most likely cause of this problem is that the JavaScript receiving end and is not able to understand the encoded UTF-8 from Perl.