[英]jQuery AJAX Request - Handle special characters in response from PHP page
I've built a simple jQuery AJAX request that queries a database for a selected template and then returns the email subject and email body and inserts those into the html input fields. 我建立了一个简单的jQuery AJAX请求,该请求查询数据库以获取选定的模板,然后返回电子邮件主题和电子邮件正文,并将其插入html输入字段。 This is all working well.
一切都很好。
I've just noticed that when the data is inserted into the email body it looks like this: 我刚刚注意到,将数据插入电子邮件正文时,它看起来像这样:
Dear Sally & John, thank you for your call today . . .
instead of: 代替:
Dear Sally & John, thank you for your call today . . .
Also there are placeholders in the email body that the user would typically change manually like this: 电子邮件正文中还有占位符,用户通常会像这样手动更改:
Dear <<dear>>,
these are appearing as: 这些显示为:
Dear <<dear>>
I'm using PHP to query the database and return the data as JSON: 我正在使用PHP查询数据库并以JSON形式返回数据:
$templateDetails[] = array('templateBody' => $updatedTemplateBody);
echo json_encode($templateDetails);
Here's the script that calls the PHP request: 这是调用PHP请求的脚本:
$(document).ready(function() {
$("#templateRef").change(function() {
var templateRef = $("#templateRef").val();
var contactID = '<?php echo $contactID; ?>';
$.post('getSMSTemplate.php', {
contactID: contactID,
templateID: templateRef
}, function(data) {
data = JSON.parse(data);
if (data.error) {
alert("error");
$("#messageBody").html('');
return; // stop executing this function any further
} else {
// console.log( data[0].templateBody );
$("#messageBody").val(data[0].templateBody);
}
}).fail(function(xhr) {
$("#messageBody").html('');
});
});
});
Is there some function that I can call to escape these characters? 我可以调用一些函数来转义这些字符吗? Not sure if this is done in JavaScript or PHP or where to start with this?
不知道这是用JavaScript还是PHP完成的,或者从何处开始?
This part: $("#messageBody").val(data[0].templateBody);
这部分:
$("#messageBody").val(data[0].templateBody);
can instead be $("#messageBody").html(data[0].templateBody);
可以改为
$("#messageBody").html(data[0].templateBody);
. 。
You can handle it serverside in your php using htmlspecialchars_decode()
which you would just call when generating your response before echo
ing your JSON back out. 您可以使用
htmlspecialchars_decode()
在php中在服务器端处理它,在生成响应之前echo
呼JSON之前将调用它。
<?php
$str = "<p>this -> "</p>\n";
echo htmlspecialchars_decode($str);
// note that here the quotes aren't converted
echo htmlspecialchars_decode($str, ENT_NOQUOTES);
?>
Output: 输出:
<p>this -> "</p>
<p>this -> "</p>
(From PHP Manual) https://secure.php.net/manual/en/function.htmlspecialchars-decode.php (摘自PHP Manual) https://secure.php.net/manual/zh/function.htmlspecialchars-decode.php
Those "special characters": they are actually HTML encoded text. 那些“特殊字符”:它们实际上是HTML编码的文本。 The issue is not in the code that you posted in your question.
问题不在您在问题中发布的代码中。 Here is a quick proof:
这是一个快速证明:
var data = "<< me & you >>" $("#messageBody").val(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="messageBody" />
As you can see the code displays the "special characters" correctly, so the issue is caused by something else and without knowing more details about your project, I can only think of two possibilities. 如您所见,代码正确显示了“特殊字符”,因此该问题是由其他原因引起的,并且在不了解有关项目的更多详细信息的情况下,我只能想到两种可能性。
1) They are saved like that in the database, so check there to confirm. 1)它们像这样保存在数据库中,因此请在此处进行确认。 In this case you can either use
htmlspecialchars_decode()
to decode your database data before you echo
it as JSON, or you can decode it in your jQuery above using $('<div/>').html(data[0].templateBody).text();
在这种情况下可以组合使用
htmlspecialchars_decode()
你之前到数据库的数据进行解码echo
它作为JSON,或者您可以在上面的jQuery使用它进行解码$('<div/>').html(data[0].templateBody).text();
. 。 Here is a test:
这是一个测试:
var data = "<< me & you >>" data = $('<div/>').html(data).text(); $("#messageBody").val(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="messageBody" />
2) The text gets encoded when you're generating the email. 2)生成电子邮件时,文本将被编码。 If the text coming from the database un-encoded and your
messageBody
is displaying it correctly, then you need to inspect the code that is grabbing the text from messageBody
to generate the email. 如果来自数据库的文本未编码,并且您的
messageBody
正确显示了它,那么您需要检查从messageBody
抓取文本的代码以生成电子邮件。 Post that code in your question if you need help with it. 如果您需要帮助,请在您的问题中张贴该代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.