jQuery AJAX 请求数据选项值中的与号 (&) 字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4226480/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ampersand (&) character inside a value of jQuery AJAX request data option
提问by Nik Sumeiko
I am performing asynchronous HTTP (Ajax) requests via jQuery with the basic $.ajax(). The code looks like the following:
我正在使用基本的 $.ajax() 通过 jQuery 执行异步 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!" );
}
});
});
Everything is working properly as usual, until user types inside textareaampersand (&) character.Than when I debug PHP function, which saves the value, it always have a value until this character.
一切都像往常一样正常工作,直到用户在textarea 中输入& 字符。比当我调试保存值的 PHP 函数时,它总是有一个值直到这个字符。
I believe there has to be a solution to skip ampersand (&) somehow. Any ideas?
我相信必须有一个解决方案来以某种方式跳过&符号(&)。有任何想法吗?
回答by Darin Dimitrov
Instead of:
代替:
data: "id=" + thisId + "&value=" + thisValue
do:
做:
data: { id: thisId, value: thisValue }
This way jquery will take care of properly URL encoding the values. String concatenations are the root of all evil :-)
这样 jquery 将处理正确的 URL 编码值。字符串连接是万恶之源:-)
回答by T.J. Crowder
Strongly recommend you use the solution provided by Darinabove if at all possible; that way, you get to reuse well-tested code for building POST
data.
如果可能,强烈建议您使用上面Darin 提供的解决方案;这样,您就可以重用经过良好测试的代码来构建POST
数据。
But if you really, really, really need to use string concatenation (here, or elsewhere in your application when building up query strings or POST
data out of user inputs), you need to use encodeURIComponent
:
但是,如果您真的,真的,真的需要使用字符串连接(此处或应用程序中的其他地方,在POST
根据用户输入构建查询字符串或数据时),则需要使用encodeURIComponent
:
$("textarea").blur(function(){
var thisId = $(this).attr("id");
var thisValue = $(this).val();
$.ajax({
type: "POST",
url: "some.php",
data: "id=" + encodeURIComponent(thisId) + "&value=" + encodeURIComponent(thisValue),
success: function(){
alert( "Saved successfully!" );
}
});
});
By default when sending a POST
with jQuery.ajax
, you're sending data with the content type application/x-www-form-urlencoded
, which means you're promising that the data is encoded that way. You have to be sure to keep your part of the bargain and actually encode it. This isn't just important for ampersands.
默认情况下,发送POST
with 时jQuery.ajax
,您发送的是内容类型为 的数据application/x-www-form-urlencoded
,这意味着您承诺以这种方式对数据进行编码。您必须确保保留自己的那部分交易并对其进行实际编码。这不仅对&符号很重要。
回答by js1568
just use the javascript function encodeURIComponent()
:
只需使用 javascript 函数encodeURIComponent()
:
$("textarea").blur(function(){
var thisId = $(this).attr("id");
var thisValue = $(this).val();
$.ajax({
type: "POST",
url: "some.php",
data: "id=" + thisId + "&value=" + encodeURIComponent(thisValue),
success: function(){
alert( "Saved successfully!" );
}
});
});
回答by Selim Destanc?
$.ajax({
type:'POST',
dataType: "text",
url:'save.php',
data:'_id='+$('#id').val()+'&title='+$('#title').val(),
??
data: { id: thisId, value: thisValue }