JSON, JavaScript, wrong Japanese Character - javascript

I have this on my JSON-file (mydata.json on the server):
"jp": [ "私はあなたを愛しています!",
and more. There is also "de" and "en" and so on and it is displayed correctly. But with these japanese characters, I get these unrecognizable characters. I have read threads here but I can't find a solution. I think it has something to do with the data from the server, wrong asking header, but don't know, how to fix it. I tried this (found here on stackoverflow) (file.js):
$.ajax({url: myURL, contentType: "application/json; charset=utf-8",
success: function(dataJSON){
console.log(dataJSON);
}
});
End the outputs of the "de" and "en" (and more) are great, but the japanese characters appear like this:
���͂��Ȃ��������Ă��܂��I
How can I fix it?

In the server you need unicode http://unicode-table.com/en/#cjk-radicals-supplement
In javascript there is no problem because it uses unicode

set Content-Type: text/html; charset=utf-8
at the beginning of the the file you request json from it

Try this!
Ajax automatically detects the dataType, but here it is explicit.
$.ajax({
url: myURL,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(dataJSON){
console.log(dataJSON);
}
});

Related

Ajax post breaks with special characters

I am trying to pass the value of a textarea to an php page to then be processed and added to a SQL database.
I want the text area to be able to support special characters.
Everything works fine till I put this string in the text area and post it:
JΛ̊KE#2##&($^#%###%))$&#("""
I am getting a 501 Not implemented error.
Now when I paste in certain PHP code into the text area (not to run, purely to save as a string), I get a 403 Forbidden error.
Why does the value of the text area affect the error code?
For now, the paste.php file has no code so that I could try and understand where the error is coming from. I am certain the error is coming from the ajax post. I've looked everywhere online but have not been able to find how to make the string safe to post. encodeURIComponent doesn't seem to work in this case.
Here is the JS for the button press:
var note = $("#note").val();
var dataString = encodeURIComponent(note);
$.ajax({
type: "POST",
url: "php/paste.php",
data: JSON.stringify({
paste: dataString
}),
dataType: 'text',
contentType: "application/json; charset=utf-8",
success: function(msg) {
alert(msg);
console.log(msg)
},
error: function(ts) {
alert(ts.responseText)
}
});
try adding this
Content-Type: application/x-www-form-urlencoded
What was needed was to use this function on the data.
function encode_utf8(s) {
return unescape(encodeURIComponent(s));
}
Change the content type to
ContentType: "application/x-www-form-urlencoded; charset=UTF-8"
and for the kicker, add this to my .htaccess file :
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>

IE8 special char in Textarea and MVC2

I have a big problem with IE8
I have field TextArea, user write everything use lot of characters ç àèì áéí, show ok in textarea, debug in Javascript and Jquery ok too.
but when I send to MVC controller change characters special for "?"
I tryed in Chrome and FF IE 10 or 11 , its ok, problem is only IE 8
Using Ajax
$.ajax({
type: "POST",
url: "url",
dataType: "json",
data: "{}",
contentType: "application/json; charset=iso-8859-1",
success: function (msg) {
alert(msg);
},
});
In the line "contetType" is possible change character type and send format what you want.

making ajax work on JSBIN

I'm trying to get ajax to work in JSBIN like demonstrated in this video. What have I don't wrong. Seems like it ought to work!
$(document).ready(function(){
$.ajax({
type: "get",
url: "http://jsbin.com/ipefom/1/js",
dataType: "json",
success: function(returnedData){
console.log(returnedData)
}
});
});
http://jsbin.com/ocerag/3/edit
I don't understand where my parseerror comes from.
Your json is invalid:-
There is as edit:8 in json which is misplaced and also you have duplicate keys while the number is repeated again. Seems like the same set was copy pasted again.
"968":"a","969":"a","970":"a","971":"a","972":"a","973":"a","974":"a","975":"a","976":"a","977":"a","978":"a","979":"a","980":"a","981":"a","982":"a","983":"a","984":"a","985":"a","986":"a","987":"a","988":"a","989":"a","990":"a","991":"a","992":"a","993":"a","994":"a","995":"a","996":"a","997":"a","998":"a","999":"a"} edit:8
{"0":"a","1":"a","2":"a","3":"a","4":"a","5":"a","6":"a","7":"a","8":"a","9":"a","10":"a","11":"a","12":"a","13":"a","14":"a","15":"a","16":"a","17":"a","18":"a","19":"a","20":"a","21":"a","2
Because it is not returning valid json, when you try
dataType: "html", in place of dataType: "json", then it will show that the returning is not a valid json.

looking for js library for formatting string to match json requirments

I'm using jQuery ajax to send updates from the client's browser to my server,
I noticed that there is some characters that JSON doesn't support,
and in order to send them, i need to add additional \ in front of each.
This characters are well documented at json.com:
(source: json.org)
I'm not so good at javascripting (i'm new in this area),
So i wonder if there is any already made function that takes a string and formats it to fit this requirement?
iv'e searched the web but couldn't find any javascript solution for this..
Thanks in advance,
Eitan.
Solved! i spent too much time on this #$!
I found that the JSON.stringify indeed take care of the special characters.
The problem was in the way that i used to structure the data...
I did it like:
blah = JSON.stringify( { a: "1" } );
$.ajax({
type: "POST",
url: "page.aspx/MethodName",
data: "{ paramName: '" + blah + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json"});
To fix this, i just did:
//nesting the param name in the data structure passed to stringify:
blah = JSON.stringify( { paramName : { a: "1" } } );
$.ajax({
type: "POST",
url: "page.aspx/MethodName",
data: blah,
contentType: "application/json; charset=utf-8",
dataType: "json"});
Thanks for everyone who tried to help me solve this..

Jquery ignores encoding ISO-8859-1

I have a website which apperently removes the correct encoding (ISO-8859-1) from a string and sends it wrong.
I have this encoding specified in my HTML
<meta charset="ISO-8859-1">
I load my javascript via
<script type="text/javascript" charset="ISO-8859-1" src="...
I send for Information via JQuery Ajax Request like this (with german special character 'ö' and 'ä'):
$.ajax({
url: '..',
type: 'POST',
contentType: 'application/xml;charset=ISO-8859-1',
data: xmlRequest.html(),...
This is translated into a request and in the chrome developer tools I see this in the Request Header:
..
Content-Type: application/xml;charset=UTF-8
..
What happened there?
Of course the special characters are encoded wrong ("ö" instead of "ö") the server can't understand me and i get an error.
Because I had the same problem, I'll provide a solution that worked for me. Background: Microsoft Excel is too stupid to export a CSV-File in charset UTF-8:
$.ajax({
url: '...',
contentType: 'Content-type: text/plain; charset=iso-8859-1',
// This is the imporant part!!!
beforeSend: function(jqXHR) {
jqXHR.overrideMimeType('text/html;charset=iso-8859-1');
}
});
According to the jQuery.ajax() contentType documentation:
Data will always be transmitted to the server using UTF-8 charset; you must decode this appropriately on the server side."

Categories