为什么在空格中用字符串替换加号
I am using ajax to send data through an AJAX call to set.php:
$.ajax({
url: "ajax/set.php",
dataType: "html",
type: 'POST',
data: "data=" + data,
success: function (result) {
alert(result);
}
});
Before sending the AJAX call, I am using JavaScript to alert()
the data, the data is:
JeCH+2CJZvAbH51zhvgKfg==
But when I use $_POST["data"]
, the data is:
JeCH 2CJZvAbH51zhvgKfg==
Which displays pluses replaced with spaces, how can I solve this problem?
我使用ajax通过AJAX调用向set.php发送数据: p>
$。ajax({
url:“ajax / set.php”,
dataType:“html”,
type:'POST',
data:“data =”+ data,
成功:函数(结果){
alert(结果);
}
});
code> pre>
在发送AJAX调用之前,我 我使用JavaScript来 alert() code>数据,数据是: p>
JeCH + 2CJZvAbH51zhvgKfg ==
code> pre>
但是当我使用 $ _ POST [“data”] code>时,数据为: p>
JeCH 2CJZvAbH51zhvgKfg ==
code> pre>
哪些显示的替换为空格,我该如何解决这个问题? p>
div>
When using $.ajax
, use an object rather than a string with the data:
option. Then jQuery will URL-encode it properly:
data: { data: data },
If you really want to pass a string, you should use encodeURIComponent
on any values that might contain special characters:
data: 'data=' + encodeURIComponent(data),
I believe you need to encode that +
with its URL Encoded value %2B
.
To do this, use the replace
method.
var data = data.replace(/\+/g, "%2B");
Taking a look at the jQuery docs, you can pass an object to data
instead of the actual query built by hand. Try:
$.ajax({
url: "ajax/set.php",
dataType: "html",
type: 'POST',
data: {
data: data
},
success: function (result) {
alert(result);
}
});