I have a https request using ajax to external php page like in the code bweloow. Please note there are some strange non UTF-8 chars that have to be encoded, otherwise i would get a strange return.
//ajax request to external site
sometext = "I only wear Rtrtrr® shoes, and my name is Åron";
sometext = some_encoding(sometext); //a PSEUDO code line
$.ajax({
type: "POST",
url: "http://targetsite.com/process_text.php",
data: sometext,
cache: false,
success: function(proc_text) {
alert()
}
});
On php page, then, I need to decode and use some_decoding(sometext) instead of some_encoding(sometext).
Is there any way to encode such text, with some_encoding function i javascript that has analog function for some decoding in php.
For example encodeURIComponent doesn't convert all the chars, while Base64 I used is not the same in PHP and Javascript in some cases. Is there something that is the same.
Related
I have a problem with encoding/decoding some text.
On my page I have a picture:
<img id ="pcs" src="xxx.jpg" game="Hellblade: Senua’s Sacrifice ">
Then use ajax to send data to php server:
$('#pcs').click(function(){
if($(this).attr('src') == "xxx.jpg" ) {
$(this).fadeOut(100, function() {
var cookie = "dummy";
var game = $(this).attr('game');
$.ajax({
url: "http://example/notif_games.php",
type: "POST",
data: ({'cook_':'cookie', 'game_': game}),
});
The problem is the data are somehow encoded and recieved as: Hellblade: Senua’s Sacrifice
Instead, I need it to be recieved as: Hellblade: Senua’s Sacrifice
Even where I try
var game = $(this).attr('game');
alert(game);
it returns the 'encoded' value.
It's needed with the ’ part because I then put it in DB and compare it with another tables where the value is with the ’.
This is a browser issue and not a jquery one. Your browser is rendering the text already before jquery touches it. You need to decode it on the server side when you ajax it up.
With php you can do $data = htmlentities($data);
I tried to develop a firefox add-on recently to handle some routines automatically.
There is some webpage composed of a lot of links, and each link points to a page which contains a form tag. When the webpage is loaded, I want to inject a script, which follows through each link in webpage and click the submit button inside.
I use $.get to get each link in the webpage, parse post data with $('form').serialize(), and click the submit button by using $.post.
The issue is that the pages I'm dealing with is big5 encoded. When jQuery loads the data, it transforms the information in the page into Unicode. And then I use $.post to send the Unicode data. But the server side only accepts data in big5 encoding.
$('a').each(function(){
$.ajax({
type: 'GET',
url: this.href,
success: function(data){
var html = $.parseHTML(data);
$.ajax({
type: 'POST',
url: $('form', html).attr('action'),
data: $('form', html).serialize(),
success: function(data){
console.log(data);
}
});
}
});
});
I have no control over the server side, so the only way I can do is to modify my add-on scripts. I have googled a lot with keywords (jQuery, encoding, big5, form) and still have no results.
Can we send data in big5 via jQuery.post? Or is there other way I can do to finish my add-on?
Thank you :)
I am trying to develop a function to send json data via get method using javascript instead of curl.
And it also works fine with the get method of jquery ajax api.For example,
$.ajax({
url : url,
type : "GET",
data : json,
dataType: "json",
//.....
//other code here.
})
But something wrong happened, when I try to send the same json data by the follow code as someone else suggest here.
xmlhttp.open("GET",url+"?pretty="+encodeURIComponent(JSON.stringify(json)),true);
xmlhttp.setRequestHeader("Content-type","application/json");
xmlhttp.send();
However,it seems that the json data is too large to be encoded inside the url, and the url was truncated.
So, I wander how I can send the large json with javascript just as the jquery does.
I'm building a chrome extension in javascript.
I'm trying to get the page contents from an Outlook mailserver through an ajax request, but I have trouble getting the correct page returned.
I suspect it is the danish letter æ, that creates the problem, but don't know how to resolve it.
$.ajax({
url: baseURL + 'Indbakke/',
data: "Cmd=contents&View=Ulæste%20meddelelser", //Avoid encodeURI
dataType: 'html',
processData: false, //Avoid encodeURI
cache: false,
success: function(data) {
fetchedInbox = $.parseHTML(data);
//If there are changes to the inbox, refresh the inbox page
if(findString(fetchedInbox, 'ingen emner'))
{
window.parent.frames[1].location = baseURL + 'Indbakke/?Cmd=contents';
}
},
complete : function(){
console.log("URL" + this.url)
}
});
The data variable of the succes function contains the wrong page, but if I copy 'this.url' from the complete function into a browser, it displays the right page. I have tried using default ajax settings and encodeURI on the full link (without using the 'data' option), but then neither 'data' or 'this.url' will work (i.e. I change the second parameter to 'View=Ul%C6ste%20meddelelser').
I do not have access to the (probably) asp page that the server sends, just javascript, so I can't do anything serverside.
Note: When chromes console show this.url, it breaks the link before æ, so I have to manually copy the url
It's probably the æ that creates this error. Replace æ by %C3%A6 and the headers will be send to the correct location.
data: "Cmd=contents&View=Ul%C3%A6ste%20meddelelser"
To find the encoded character, I used this converter: http://meyerweb.com/eric/tools/dencoder/
A client-side script takes the text within a text input, "wraps" it within an XML block and sends it to a server that stores the information in a MySQL database.
As a first step before wrapping the input value, I escape the "&" characters like so:
var copyright = copyright.replace(/&/g,"&");
The resulting XML data block is sent to the server using jquery's ajax method:
var copyright = copyright.replace(/&/g,"&"),
xml = "<request><session>"+session+"</session><space>"+space_id+"</space><view>"+view_id+"</view><copyright>"+copyright+"</copyright></request>",
url = "hidden URL";
$.ajax({
type: "POST",
url: url,
contentType: "text/xml; charset=UTF-8",
dataType: "xml;charset=UTF-8",
data: xml
});
Later after this operation, the content that was previously saved within the database needs to be retrieved and displayed within a web page:
$.ajax({
type: "POST",
url: url,
dataType: 'xml',
data: xmlString,
success: function(xml) {
var XML = $(xml);
// Process the data retrieved
},
error: function(jqXHR, textStatus, errorThrown) {
var XML = $(jqXHR.responseText);
console.log("error: "+textStatus+"\n"+errorThrown);
}
});
If an ampersand was typed in the input field and then saved, when trying to load the page that displays the same previously saved content, the ajax call breaks and runs down the error event handler, with the following error:
error: parsererror
Error: Invalid XML: <?xml version="1.0" encoding="UTF-8"?><response><target>
<target_id>2095466</target_id>
<plot>20029/13</plot>
<builder>Lemminkäinen</builder>
<housing_form>vm</housing_form>
<block_name></block_name>
<finnish_year>2013</finnish_year>
<target_name>As Oy Helsingin Saukonranta</target_name>
<target_address>Saukonpaadenranta 8</target_address>
<office_space></office_space>
<purpose></purpose>
<reservations></reservations>
<contacts></contacts>
<infoflag>2</infoflag>
<views>
<view>
<view_id>2095468</view_id>
<copyright>B&M</copyright>
</view>
</views>
</target>
<status>OK</status><errormsg></errormsg></response>
What is it that I'm doing wrong? Am I escaping the characters wrongly, or is it something else?
This question may seem to be a duplicate, but to me it doesn't seem like it since the ampersand characters have been escaped prior to being stored. I even tried adding additional (1, then two) amp;s to the escape string, but the result is EXACTLY the same.
It turns out that the problem actually came from the server (to which I did not have access), the script that handled the requests did not escape the ampersand characters correctly, even though they were on the client-side.
Bellow is a JavaScript function that escapes all (?) special characters used with XML, just in case someone needs it:
function escapeXML(string){
var str = string;
str = str.replace(/\&/g,"&");
str = str.replace(/\>/g,">");
str = str.replace(/\</g,"<");
str = str.replace(/\"/g,""");
str = str.replace(/\'/g,"'");
return str;
}
The problem is the ä character of the Lemminkäinen in the builder node, as pointed by Shahid. When the Lemminkäinen text is UTF-8 decoded, the ä would be part of a two-characters UTF-8 encoding. So the UTF-8 decoder would try to decode äi, which is not a valid character sequence. The correct UTF-8 encoded character for ä is ä, or 0xC3, 0xA4 in binary. Thus, the full UTF-8 encoded text should be Lemminkäinen.
When the reported XML data is saved in an XML file then opened with a web browser, it'll fail on all major web browsers: Chrome ("Encoding error"), Firefox ("not well-formed"), Safari ("Encoding error"), MSIE ("An invalid character was found in text content."), and Opera ("illegal byte sequence in encoding").
Since the XML data came from the server, it's likely that the script that posted the builder data didn't specify an UTF-8 character set (there's no indication that the provided codes are the one that does it). It may have caused by old script which by now, is already fixed, but the damage has already been done. i.e.: incorrect data format was added into database. Manual input into database is also a possible cause during server maintenance.