jquery AJAX response in plain ASCII - ajax

I post a pair of chinese characters to my server:
忠孝
the web server then translates them into pinyin:
Zhōngxiào
I know this works, because I got that by directly hitting my AJAX handler. So, the server is encoding everything correctly. However, when I receive that response via jQuery, I get:
Zhōngxiào
My code:
$.ajax({
type: "POST", url: 'procChineseTrans', data: 'toTrans='+text,contentType: "application/x-www-form-urlencoded; charset=UTF-8",
complete: function(data){
//alert(data.responseText);
$('#street_address').val(data.responseText);
}});
I also set:
header('Content-Type:text/html; charset=UTF-8');
and
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Is there a reason my response is in plain ASCII?
Edit:
Checking my response header, it says it is in UTF-8. But, it is still displaying ASCII

Aha...I see what your problem is. The text/html header you're sending back is causing jquery to parse the responseText as HTML. When you don't specify the dataType option, jquery makes a best guess as to what type of data you're playing with based on response headers.
Try adding dataType : text to the options in your $.ajax call. If that works, then think about adjusting the response Content-type header.
For further reading, search for "dataType" in the jquery manual page : http://api.jquery.com/jQuery.ajax/

Forced it to into html() which formatted it.

Related

HTML5/JSON : UTF-8 cant read proper characters from json

My HTML5 app is using JSON to fetch data.
The json contact characters like: ö
but the output in my browser is like: �
I used the following in HTML5 header:
<meta http-equiv="Content-Type"content="text/html;charset=utf-8">
and used this in the AJAX to read the JSON:
$.ajax({
url: "items.json",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(response) {
..........
But its not helping me out. What will be the solution to it?
I even tried "ISO-8859-1" on both places.
Make sure your font family that you are using for your site supports the extended characters you are attempting to print. Then use firebug or something similar to view what character set your backend json service is sending back. It may not be sending it back. Also, check to see if firebug is showing the correct character in the JSON response. It's possible that the problem is even further back than you thought.

Ajax result is getting encoded

I have a page where onload I issue several POST requests simultaneously in order to load widgets on the page. The request looks something like this:
$.ajax({
type: 'POST',
contentType: 'application/json',
dataType: 'html',
data: JSON.stringify({}),
success: function(htmlResult) {
$div.html(htmlResult);
}
})
Most of the time everything loads fine. However, some of the time one of the widgets will get loaded with a replacement character. It's not one specific widget that this happens to, but it could be any one of them at random.
When I inspect the bad HTTP response, I notice that the Vary header has a value of Accept-Encoding and the body is encoded. All the other responses have a value of * for that header, and the body is text/html. I'm not explicitly setting the Vary header value anywhere.
I can't figure out what's causing this random behavior. Any ideas?
FYI, I'm posting to an ASP.NET MVC action, and returning a partial view.
have you tried dataType:'json' instead of 'html'?

jQuery AJAX JSON dataType Conversion

Hopefully that title isn't too cryptic. What's happening is I have a jQuery AJAX script that I'm trying to use to access an API on a remote server, which returns a JSON response. However, the API returns the JSON as MIME type "text/html" (in the response header) instead of "application/json". It would seem obvious that I simply need to change the returned content type from text to JSON, to make the AJAX call interpret the data correctly.
Unfortunately, this is not the case. I have tried this in a multitude of different ways, all of which fail. The closest I've gotten to getting this API call to work is when the debugger tells me "Resource interpreted as Script but transferred with MIME type text/html". And the AJAX call errors out with my debug message that dumps the jqXHR object in JSON format, which tells me: {"readyState":4,"status":200,"statusText":"parsererror"}
Here is an example of my code (although I have changed the code many various ways, in my attempts at getting it to work, but this version seems to be the closest to correct):
$.ajax({
type: 'GET',
url: 'http://username:api-key#www.kanbanpad.com/api/v1/projects.json',
contentType: 'application/json',
dataType: 'jsonp',
converters: {
'jsonp': jQuery.parseJSON,
},
success: function(data) {
alert(data);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log(JSON.stringify(jqXHR));
console.log(textStatus+': '+errorThrown);
}
});
If anyone can figure out what I need to do differently to make this work, I will be extremely grateful.
It may also be worth noting that if you copy/paste the API URL into a browser address bar and hit go, it gives the proper JSON response with the proper response header ("application/json")
So unless Kanbanpad updates their API, it cannot be directly accessed with JS. You will have to use PHP (or some other) to handle the requests.
It works just as well, it just requires an extra step is all.
Just for anyone that was looking for a solution.
dataFilter(data, type)Function
A function to be used to handle the raw response data of XMLHttpRequest.
This is a pre-filtering
function to sanitize the response. You should return the sanitized data. The function
accepts two arguments: The raw data returned from the server and the 'dataType' parameter.
I would change the content type in the dataFilter interceptor to json. Bear in mind this affects all ajax calls, so use info from data to decide which ones you want to convert.
Verify that your server is sending a jsonp response. This means the json should be enclosed with a string of your callback.
The callback name is passed in the parameters, and if you're not setting it explicitly, looks something like: jQuery15102810791094068532_1300988427891 (As per http://www.json-p.org/)
On your server, you need to format the response:
jQuery15102810791094068532_1300988427891({...json response ...});
Where you use the callback defined in your GET parameter 'callback'.
You might try setting the type to "json" and see if it works. I've had a number of parsererror's with the jquery's jsonp - you might try http://code.google.com/p/jquery-jsonp until it's a bit smoother.
Try changing your content-type to this
contentType: "application/json; charset=utf-8",

jQuery-Ajax JSON charset conflict

I have two comboboxes "A" & "B". Combo "B" is populated using jQuery Ajax (dataType:json) when a value in Combo "A" is selected (onchange event).
There are cases where part of the data in "B" can be chinese/international, in which case the data appears as "????" in the browser.
Typically the entire setup is like so:
ERP <---> Servlet <---> JSP <---> Browser
ERP is UTF-8 enabled. I can clearly see the data in chinese in the ERP console. I've dumped the resultant data that passes into the servlet in a file just to check if it's proper. It's perfectly encoded. I've set the contentType for JSPs to UTF-8. Everything's in place.
I've added the necessary contentType in Ajax to "application/json;charset=utf-8". Still no dice.
That leaves the browser. I've used every browser there is and the same issue arises. I've noticed that the browser simply isn't able to understand the charset of the chinese data when populated on-the-fly.
What can possibly be going wrong? Due to security reasons I cannot post the code. I'd be grateful for any sort of advice.
Thanks a heap!
~Sabier
If you have set your JSP as UTF-8 and your resulting contentType is set to UTF-8 also, you could try this
1) What about using contentType params when you call the servlet, as shown in https://stackoverflow.com/a/6283111/1078487
$.ajax({
type: "POST",
url: "yourservlet",
dataType: "text",
data: {yourparams},// here we def wich variabe is assiciated
contentType: "application/x-www-form-urlencoded; charset=UTF-8",
success: function(data) {
//population
}
});
2) Even if your ERP is setted as UTF-8, just double check those returning string using a UTF-8 byte conversion and see what happens.
byte[] utf8Bytes = stringToParse.getBytes("UTF8");
String stringToReturn = new String(utf8Bytes, "UTF8");

Dealing with ISO-encoding in AJAX requests (prototype)

I have a HTML-page, that's encoded in ISO-8859-1 and a Prototype-AJAX call that's build like this:
new Ajax.Request('api.jsp', {
method: 'get',
parameters: {...},
onSuccess: function(transport) {
var ajaxResponse = transport.responseJSON;
alert(ajaxResponse.msg);
}
});
The api.jsp returns its data in ISO-8859-1. The response contains special characters (German Umlauts) that are not displayed correctly, even if I add a "encoding: ISO-8895-1" to the AJAX-request. Does anyone know how to fix this?
If I call api.jsp in a new browser window separately the special characters are also corrupt. And I can't get any information about the used encoding in the response header. The response header looks like this:
Server Apache-Coyote/1.1
Content-Type application/json
Content-Length 208
Date Thu, 29 Apr 2010 14:40:24 GMT
Notice: Please don't advice the usage of UTF-8. I have to deal with ISO-8859-1.
Just found the answer myself. Though this is for PHP I'm sure you can find the equivalent for ASP :)
Basically, just include the encoding header on your response page (in your case api.asp), like this:
header("Content-Type: text/html; charset=ISO-8859-1");
Good luck with it :)
//Jannik Olsen

Resources