gpt4 book ai didi

php - 通过 AJAX 将 HTML 发送到服务器端 PHP

转载 作者:可可西里 更新时间:2023-10-31 22:16:04 24 4
gpt4 key购买 nike

我正在尝试将表单中的 HTML 发送到服务器端脚本,以便将其保存在数据库中。我发送的是一个查询字符串,每当 HTML 包含逗号或 & 符号等字符时,HTML 的其余部分都会在该点被 chop 。

提前致谢!

最佳答案

发送请求时,您应该对参数进行适当的 URL 编码:

$.ajax({
url: 'foo.php',
data: { html: '<html>You can use whatever characters you want here</html>' },
type: 'GET',
success: function(result) {

}
});

或:

$.ajax({
url: 'foo.php',
data: { html: $('#someTextFieldWhichMightContainHtml').val() },
type: 'GET',
success: function(result) {

}
});

现在您可以安全地读取 PHP 脚本中的 html 变量:$.GET["html"]

我想现在你的代码看起来像这样:

$.ajax({
url: 'foo.php?html=' + $('#someTextField').val(),
type: 'GET',
success: function(result) {

}
});

我建议您永远不要使用字符串连接,而始终使用 data 散列。

关于php - 通过 AJAX 将 HTML 发送到服务器端 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4343386/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com