jQuery AJAX请求数据选项的值内的&字符 [英] Ampersand (&) character inside a value of jQuery AJAX request data option
问题描述
我正在使用基本的$ .ajax()通过jQuery执行异步HTTP(Ajax)请求.该代码如下所示:
I am performing asynchronous HTTP (Ajax) requests via jQuery with the basic $.ajax(). The code looks like the following:
$("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 和号(&)字符内输入内容.在此字符之前有一个值.
Everything is working properly as usual, until user types inside textarea ampersand (&) character. Than when I debug PHP function, which saves the value, it always have a value until this character.
我认为必须有一种解决方案,以某种方式跳过&"号.有什么想法吗?
I believe there has to be a solution to skip ampersand (&) somehow. Any ideas?
推荐答案
而不是:
data: "id=" + thisId + "&value=" + thisValue
要做:
data: { id: thisId, value: thisValue }
通过这种方式,jquery会正确地对值进行URL编码.字符串串联是万恶之源:-)
This way jquery will take care of properly URL encoding the values. String concatenations are the root of all evil :-)
这篇关于jQuery AJAX请求数据选项的值内的&字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!