I am writing a little email server. Client side is written in javascript/jquery and server side in nodeJS (this is a homework assignment)
On the client side I send an email as follows
function send() {
var dataString = 'to=' + $("#to").val() + '&subject=' + $("#subject").val() + '&body=' + $("#body").val();
var lines = $("#body").val().split("\n");
for (var i = 0;i < lines.length;i++ ) {
dataString += '&line' + i + '=' + lines[i];
}
$.ajax({
type: "POST",
url: "/send",
cache: false,
data: dataString,
success: function(data) {
location.reload();
},
error: function(xhr, textStatus, errorThrown) {
location.reload();
}
});
}
Where $("body") is a textarea element. I outputed body on the client and it was indeed Hebrew. I retrieve emails with this function:
function getMail() {
$("#navigationButtons").show();
$("#workingAread").empty();
var dataString = 'start=' + start + '&url=' + url;
$.ajax({type: "GET", data: dataString, dataType:"json", url: 'getMail', cache: false})
.done(function(data){
mails = data;
$("#workingArea").empty();
if (mails.length === 0) {
$("#workingArea").html("Your inbox is empty");
}
var mailContainer = $("<div></div>");
numOfMails = mails.pop();
mails.forEach(function(elem,i){
var mail = $("<span></span>");
mail.text(elem.fullName + ", " + elem.date + ": " + elem.subject);
mail.click(showMail.bind(this,i));
var chk = $("<input></input>")
.attr("type","checkbox")
.attr("id",elem.uuid);
mailContainer.append(chk).append(mail);
mailContainer.append("<br>");
});
$("#workingArea").append(mailContainer);
return;
})
}
As you can see dataType is set to json. Problem is I get gibberish, mostly questions marks.
Does anyone have an idea on how to solve it?
Thanks!
Make sure that your HTML page is encoded as UTF-8, that your server sends UTF-8, and that your emails are stored as UTF-8.