Problem with ajax and posting non-latin characters - mysql

Posting non-latin based languages with ajax + jquery doesn't save to mysql the correct text.
What I have done is this:
I am getting multiple translated words from Google's translation api.
The ajax request is showing the correct translations for all languages.
But when i try and insert this into the db it shows up in php my admin as garbled text
I added AddDefaultCharset UTF-8 to .htaccess file on the root.
I tried setting the header in php to utf-8 and this did not work.
I have tried adding a contentType to ajax setup but this didn't work also.
I am using the following jquery code:
I am able to see the translated text sent to the save_translation.php page
var d = {"english":"<?php echo $w;?>","addwords":translated};
data = jQuery.param(d);
$.ajax({
type:"POST",
data:data,
url:"save_translation.php"
});
Each field is set to utf8_general_ci
UPDATED : responses:
Ajax post
english:thank you addwords:baie
dankie|falemnderit|شكرا|дзякуй|благодаря|gràcies|谢谢|谢谢|謝謝|hvala
vam|děkuji|tak|dank u|thank you|tänan
teid|salamat|kiitos|merci|grazas|Danke|σας
ευχαριστώ|תודה|धन्यवाद|köszönöm|þakka þér|terima kasih|go raibh maith
agat|grazie|ありがとう|감사합니다|paldies|ačiū|Ви благодарам|terima
kasih|nirringrazzjak|takk skal du ha|تشکر از
شما|dziękuję|obrigado|mulţumesc|спасибо|хвала|ďakujem|hvala|gracias|asante|tack|salamat|คุณขอบคุณ|teşekkür
ederim|спасибі|cảm ơn bạn|ddiolch 'ch|אַ דאַנק
Server Side ouput
english : thank you baie
dankie|falemnderit|شكرا|дзякуй|благодаря|gràcies|谢谢|谢谢|謝謝|hvala
vam|děkuji|tak|dank u|thank you|tänan
teid|salamat|kiitos|merci|grazas|Danke|σας
ευχαριστώ|תודה|धन्यवाद|köszönöm|þakka þér|terima kasih|go raibh maith
agat|grazie|ありがとう|감사합니다|paldies|ačiū|Ви благодарам|terima
kasih|nirringrazzjak|takk skal du ha|تشکر از
شما|dziękuję|obrigado|mulţumesc|спасибо|хвала|ďakujem|hvala|gracias|asante|tack|salamat|คุณขอบคุณ|teşekkür
ederim|спасибі|cảm ơn bạn|ddiolch 'ch|אַ דאַנק|
Each field is set to utf8_general_ci
PhpMyAdmin
thank you|baie
dankie|falemnderit|شكرا|дзÑкуй|благодарÑ|grÃ
cies|谢谢|谢谢|è¬è¬|hvala vam|dÄ›kuji|tak|dank u|thank you|tänan
teid|salamat|kiitos|merci|grazas|Danke|σας
ευχαÏιστώ|תודה|धनà¥à¤¯à¤µà¤¾à¤¦|köszönöm|þakka
þér|terima kasih|go raibh maith
agat|grazie|ã‚ã‚ŠãŒã¨ã†|ê°ì‚¬í•©ë‹ˆë‹¤|paldies|aÄiÅ«|Ви
благодарам|terima kasih|nirringrazzjak|takk skal du
ha|تشکر از
شما|dziÄ™kujÄ™|obrigado|mulÅ£umesc|ÑпаÑибо|хвала|Äakujem|hvala|gracias|asante|tack|salamat|คุณขà¸
บคุณ|teÅŸekkür ederim|ÑпаÑибі|cảm Æ¡n bạn|ddiolch
'ch|×Ö· ד×Ö·× ×§

Probably it will be enough to use JavaScript function encodeURIComponent.
If you don't add parameters manual to the URL like myUrl + '?param1=' + param1 + '&param2' + param2, but use construction myUrl + '?' + jQuery.param({param1:param1, param2:param2}) then encoding with respect of the function function encodeURIComponent will make jQuery for you. In the case all '&' characters and 'paramX=' strings will be also added for you.
But the best way to use data parameter of the jQuery.ajax method. If you use
jQuery.ajax({
url: myUrl,
data: {param1:param1, param2:param2},
//...
});
then your URL will be appended with '?param1=' + param1 + '&param2' + param2 with the corresponding encoding full from jQuery.
If my tips not helps you, please post your code example.
UPDATED: After you posted test data it was clear, that you have no problem with ajax request. You show that the data of the server look like absolutely correct. So your problem is somewhere between PHP and MYSQL. I works with no from this two products and can now not really help you. Probably information from the following link could help you string issue of utf-8 encoding with PHP and MySQL?. You can try also
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
I wish you much success and quick solving of your problem.
Regards
P.S. In your ajax request you can directly use data: {"english":"<?php echo $w;?>","addwords":translated}. The call jQuery.param will make jQquery for you because the type of data is not a string. But it's absolutely independ on your problems.

Related

Lua Roblox HttpService:PostAsync Stripping Json

I'm trying to validate against an external API that uses hashing against the body + API.
In Roblox I have:
body = '{"attributes":[{"name":"player_level","value":120},{"name":"is_whale","value":true,"data_type":"boolean"}]}'
Which I then call this:
local attributeData = game.HttpService:PostAsync(encoded_url,body,Enum.HttpContentType.ApplicationJson,false,headers)
But in my logs:
19 Aug 2022 20:44:34.8862022-08-19 20:44:34 +0000 severity=INFO, method=POST path=/api/v1/appuser/toms_awesome_rblx_guy1/attributes format=json controller=Api::V1::Appuser::AttributesController action=create status=0 duration=1.09 view=0.00 db=0.00 ip= attributes=[{"name"=>"player_level", "value"=>120}, {"name"=>"is_whale", "value"=>true, "data_type"=>"boolean"}] session_id=62f52645795301001985e2d7 enc=0fea853f09e361b9d80579d99acda0a69c8e3c7045a2834d30b03e650a5d5f86 appuser_external_id=toms_awesome_rblx_guy1
Notice how the {}'s were stripped off to become this: attributes=[{"name"=>"player_level", "value"=>120}, {"name"=>"is_whale", "value"=>true, "data_type"=>"boolean"}]
This is ultimately causing issues with validation because the hash was made on my end with the {}'s.
Any ideas where I'm going wrong? I've tried different combinations of Enum.HttpContentType.ApplicationUrlEncoded and "data="..body and encoded_body = game.HttpService:JSONEncode(body)
But they all seem to append things in front of the generated json in the logs like data= or _json=. None of which were in my original hash based on the body json string I wouldn't expect to change.

How to set dynamic JSON String in Thymeleaf

I use Thymeleaf in frontend pages, and for some reason I have to deal with i18n by i18next.js instead of spring framework.
According to the i18next-jquery introduction - using options in translation function, I write like this:
<span data-i18n="myKey" data-i18n-options='{ "WHERE": "TW" }'></span>
And there is my language resource:
{ "myKey" : "User is from {{WHERE}}." }
And html is parsed and showed like this perfectly:
<span data-i18n="myKey" data-i18n-options='{ "WHERE": "TW" }'> User is from TW. </span>
But when I set {{WHERE}} dynamically by Thymeleaf attributes, it just can't be parsed anything.
I tried
th:attrappend="data-i18n-options='{WHERE: '+ ${country} +'}'" also
th:data-i18n-options="'{WHERE: '+ ${country} +'}'",
somehow it just end up like this:
<span data-i18n="myKey" data-i18n-options="{WHERE: TW}">User is from .</span>
So I wonder that is there any idea to parse json string in Thymeleaf attribute ?
Update:
I want to share my solution,and I hope it will save someone's time :)
th:attrappend='data-i18n-options=|{ "WHERE":"${country}"}|'
or
th:data-i18n-options='|{ "WHERE":"${country}"}|'
Please try these two:
th:attrappend='data-i18n-options=|{ "WHERE":"${country}"}|'
or
th:data-i18n-options='|{ "WHERE":"${country}"}|'

How to send MarkDown to API

I'm trying to send Some Markdown text to a rest api. Just now I figure it out that break lines are not accepted in json.
Example. How to send this to my api:
An h1 header
============
Paragraphs are separated by a blank line.
2nd paragraph. *Italic*, **bold**, and `monospace`. Itemized lists
look like:
* this one
* that one
* the other one
Note that --- not considering the asterisk --- the actual text
content starts at 4-columns in.
> Block quotes are
> written like so.
>
> They can span multiple paragraphs,
> if you like.
Use 3 dashes for an em-dash. Use 2 dashes for ranges (ex., "it's all
in chapters 12--14"). Three dots ... will be converted to an ellipsis.
Unicode is supported. ☺
as
{
"body" : " (the markdown) ",
}
As you're trying to send it to a REST API endpoint, I'll assume you're searching for ways to do it using Javascript (since you didn't specify what tech you were using).
Rule of thumb: except if your goal is to re-build a JSON builder, use the ones already existing.
And, guess what, Javascript implements its JSON tools ! (see documentation here)
As it's shown in the documentation, you can use the JSON.stringify function to simply convert an object, like a string to a json-compliant encoded string, that can later be decoded on the server side.
This example illustrates how to do so:
var arr = {
text: "This is some text"
};
var json_string = JSON.stringify(arr);
// Result is:
// "{"text":"This is some text"}"
// Now the json_string contains a json-compliant encoded string.
You also can decode JSON client-side with javascript using the other JSON.parse() method (see documentation):
var json_string = '{"text":"This is some text"}';
var arr = JSON.parse(json_string);
// Now the arr contains an array containing the value
// "This is some text" accessible with the key "text"
If that doesn't answer your question, please edit it to make it more precise, especially on what tech you're using. I'll edit this answer accordingly
You need to replace the line-endings with \n and then pass it in your body key.
Also, make sure you escape double-quotes (") by \" else your body will end there.
# An h1 header\n============\n\nParagraphs are separated by a blank line.\n\n2nd paragraph. *Italic*, **bold**, and `monospace`. Itemized lists\nlook like:\n\n * this one\n * that one\n * the other one\n\nNote that --- not considering the asterisk --- the actual text\ncontent starts at 4-columns in.\n\n> Block quotes are\n> written like so.\n>\n> They can span multiple paragraphs,\n> if you like.\n\nUse 3 dashes for an em-dash. Use 2 dashes for ranges (ex., \"it's all\nin chapters 12--14\"). Three dots ... will be converted to an ellipsis.\nUnicode is supported.

reHow to send a set of string in single variable through url

I want to send a value in URL that value contains more than one words i am using the given concept for example
<a href=page.jsp?variable1=value1&variable2=value2>click here</a>
Suppose in above value value1=aa and value2=bb cc dd
but in the url of page.jsp i am getting value1=aa and value2=bb only and the rest value "cc dd" is missing.
what should i do to get complete value for example value2=bb cc dd
I am giving here my code after making it more simple to focus on desire problem
`<%
MongoClient mongo = new MongoClient("localhost",27017);
DB database = mongo.getDB("studentDB");
DBCollection collection = database.getCollection("AskQuestion");
DBCursor cursor = collection.find();
String bodycontent="";
while(cursor.hasNext())
{
DBObject str=cursor.next();
bodycontent+="<table><tr><td><div> "+ str.get("TITLE") +"</div></td></tr></table>";
}
out.print(bodycontent);
%>`
For example str.get("_id") gives value "55093da9223da86a0212b364" and
str.get("TITLE") gives value "Question Title" .
Now my problem is i got value in Answer.jsp for str.get("TITLE") is only "Question" but not "Title" and i want the full value i.e Question Title.
I hope i am clear with my problem.
Try encoding your second variable and then attach it.
example: bb%20cc%20dd
<a href=page.jsp?variable1=value1&variable2=bb%20cc%20dd>click here</a>
You need to use java script for encoding your URL see this question for more details.
Passing a URL as a GET parameter in Javascript
Edit
See these answers
How to URL encode a URL in JSP?
http://www.coderanch.com/t/521213/JSP/java/encoding-URL-href-element-JSP

Can I retrieve more than 20 post title names on Tumblr? (via JSON)

I made a script to retrieve the strings of all the post titles of one of my blogs. The problem is that i can't retrieve more than 20 title.
Theoritically, http://www.tumblr.com/docs/en/api/v1 says: "The most recent 20 posts are included by default. You may pass these optional GET parameters: .... num - The number of posts to return. The default is 20, and the maximum is 50. ..... "
so should i be able to get the maximum 50?
My GET code looks like this:
$.ajax({
url: "http://api.tumblr.com/v2/blog/var-lak.tumblr.com/posts?api_key=(my-api-key)text?limit=50&format=text",
dataType: 'jsonp',
success: function(results){
//.....
Thanks in advance,
Attila
According to the documentation, you are passing wrong URL there. GET parameters mentioned in the documentation are separated by & sign in the address, so in:
http://api.tumblr.com/v2/blog/var-lak.tumblr.com/posts?api_key=(my-api-key)text?limit=50&format=text
You are passing to the server following GET variables:
api_key = (my-api-key)text?limit=50
format = text
By the docs to v2 API, you have to pass limit variable, so you should provide:
api_key = (my-api-key)
limit = 50
format = text
That means the URL should look like:
http://api.tumblr.com/v2/blog/var-lak.tumblr.com/posts?api_key=(my-api-key)&limit=50&format=text
This should work.