Manipulation paramètre json
Bonjour,
Je rencontre un problème pour formater un paramètre json que j'incorpore dans un mail.
J'ai un formulaire ou à la validation, j'envoie un mail de confirmation de prise en compte de la demande.
J'arrive bien à récupérer les champs que je veux mais j'ai un petit souci concernant le formatage d'une URL.
Mon JS pour récupérer les différents paramètres json :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| function registerTraining() {
var path = $(location).attr('href');
var capchallenge = Recaptcha.get_challenge();
var capresponse = Recaptcha.get_response();
try {
var parameters = '{ "url" : "' + cleanJSONString(path) +
'", "firstname" : "' + cleanJSONString($("#inputFirstname").val()) +
'", "lastname" : "' + cleanJSONString($("#inputLastname").val()) +
'", "email" : "' + cleanJSONString($("#inputEmail").val()) +
'", "company" : "' + cleanJSONString($("#inputCompany").val()) +
'", "job" : "' + cleanJSONString($("#inputJob").val()) +
'", "address1" : "' + cleanJSONString($("#inputBillingAddress1").val()) +
'", "address2" : "' + cleanJSONString($("#inputBillingAddress2").val()) +
'", "trainingname" : "' + cleanJSONString(GetQueryStringParams("n")) +
'", "trainingdate" : "' + cleanJSONString(GetQueryStringParams("d")) +
'", "message" : "' + cleanJSONString($("#inputMessage").val()) +
'", "captchachallenge" : "' + capchallenge +
'", "captcharesponse" : "' + capresponse + '" }';
$.ajax({
type: "POST",
url: '_vti_bin/json/test.svc/registertraining',
contentType: "application/json; charset=utf-8",
dataType: 'json',
data: parameters,
success: function (msg) {
registerTrainingSucceeded(msg);
},
error: registerTrainingFailed
});
}
catch (e) {
alert('Error invoking service' + e);
}
Recaptcha.reload();
} |
Je fais appel à mon WebControl pour l'envoi du mail : pas de souci de ce côté.
Mais dans le mail, je retrouve mon champs "Trainingname" de ce type :
- Risk%20management%20under%20UCITS%20%3A%20legal%20and%20regulatory%20framework /
J'aimerais pouvoir formater ce champs pour le recevoir sans les caractères spéciaux.
Dans mon WebControl, voila comment je construis mon mail :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| /// <summary>
/// Writes confirmation RegisterTraining e-mail
/// </summary>
private string BuildConfirmationRegisterTrainingBody(string firstname, string lastname, string email, string contactemail, string trainingname, string trainingdate)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("Confirmation : Register for training");
sb.AppendLine();
sb.AppendLine(string.Format("To : {0} ({1} {2})", email, firstname, lastname));
sb.AppendLine(string.Format("Training name : {0} / Training date : {1}", trainingname, trainingdate));
sb.AppendLine();
sb.AppendLine("We acknowledge receipt of your email. Thank you very much for your interest in our training programme. Our administrative department will contact you in the next 24 hours to confirm your registration.");
sb.AppendLine();
sb.AppendLine("Best regards,");
return sb.ToString();
} |
J'ai pourtant ajouté une fonction JS pour cleaner les valeurs que j'ai :
Code:
1 2 3 4 5 6 7 8 9 10 11
| function cleanJSONString(str)
{
//clean values in json properties so as to send a valid json request
return str.replace(/[/\\""]/g, "")
.replace(/\\n/g, "\\n")
.replace(/\\&/g, "\\&")
.replace(/\\r/g, "\\r")
.replace(/\\t/g, "\\t")
.replace(/\\b/g, "\\b")
.replace(/\\f/g, "\\f");
} |
Mais j'ai toujours les "%20...etc." de mon paramètre "trainingname".
Comment je dois m'y prendre pour enlever les "%20...etc." ?