我正在通过 jQuery 使用基本的 $.ajax() 执行异步 HTTP (Ajax) 请求。代码如下所示:
$("textarea").blur(function(){
var thisId = $(this).attr("id");
var thisValue = $(this).val();
$.ajax({
type: "POST",
url: "some.php",
data: "id=" + thisId + "&value=" + thisValue,
success: function(){
alert( "Saved successfully!" );
}
});
});
一切都像往常一样正常工作,直到用户在 textarea 中键入与号 (&) 字符。比我调试保存值的 PHP 函数时,它总是有直到该字符为止的值。
我相信必须有一个解决方案以某种方式跳过&符号(&)。有什么想法吗?
最佳答案
而不是:
data: "id=" + thisId + "&value=" + thisValue
做:
data: { id: thisId, value: thisValue }
这样 jquery 就会对值进行正确的 URL 编码。字符串连接是万恶之源:-)
关于jquery - jQuery AJAX 请求数据选项值内的与号 (&) 字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4226480/