I am using Ajax POST method to send data, but i am not able to send '+'(operator to the server i.e if i want to send 1+ or 20k+ it will only send 1 or 20k..just wipe out '+') HTML code goes here..
我正在使用Ajax POST方法发送数据,但是我不能发送'+'(操作员)到服务器I。如果我想发送1+或20k+,它只会发送1或20k。删除'+')HTML代码到这里。
<form method='post' onsubmit='return false;' action='#'>
<input type='input' name='salary' id='salary' />
<input type='submit' onclick='submitVal();' />
</form>
and javascript code goes here,
javascript代码在这里,
function submitVal()
{
var sal=document.getElementById("salary").value;
alert(sal);
var request=getHttpRequest();
request.open('post','updateSal.php',false);
request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
request.send("sal="+sal);
if(request.readyState == 4)
{
alert("update");
}
}
function getHttpRequest()
{
var request=false;
if(window.XMLHttpRequest)
{
request=new XMLHttpRequest();
}
else if(window.ActiveXObject)
{
try
{
request=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
request=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
request=false;
}
}
}
return request;
}
in the function submitVal() it first alert's the salary value as it is(if 1+ then alerts 1+), but when it is posted it just post's value without '+' operator which is needed... is it any problem with query string, as the PHP backend code is working fine...
在函数submitVal()中,它的第一个警告是它的工资值(如果1+然后提醒1+),但是当它被发布时,它只是post的值,而不需要“+”操作符,这是需要的……对于查询字符串是否有任何问题,因为PHP后端代码运行良好……
2 个解决方案
#1
3
Use
使用
request.send("sal="+encodeURIComponent(sal));
The + is interpreted as an space on the server side so you need to encode the string first.
+被解释为服务器端的一个空间,因此您需要首先对字符串进行编码。
More info here:
更多信息:
http://xkr.us/articles/javascript/encode-compare/
http://xkr.us/articles/javascript/encode-compare/
#2
1
You need to encode sal in request.send("sal="+sal). You'll probably find that if sal was equal to "foo & bar", you would end up with just "foo" on the server since the & needs to be encoded too. So:
您需要在request.send(“sal=”+sal)中对sal进行编码。您可能会发现,如果sal等于“foo & bar”,那么服务器上就只剩下“foo”,因为&也需要编码。所以:
request.send("sal=" + encodeURIComponent(sal)); // updated example
However, rather than doing this all by hand, you should think about using a library to do it for you, such as jQuery, then your example would look something like this:
然而,与其手工完成这些工作,不如考虑使用一个库来完成,比如jQuery,那么您的示例应该是这样的:
$.ajax({
url: "updateSal.php",
type: "POST",
data: { sal : $("salary").val() },
success: function(){
alert("update");
}
});
#1
3
Use
使用
request.send("sal="+encodeURIComponent(sal));
The + is interpreted as an space on the server side so you need to encode the string first.
+被解释为服务器端的一个空间,因此您需要首先对字符串进行编码。
More info here:
更多信息:
http://xkr.us/articles/javascript/encode-compare/
http://xkr.us/articles/javascript/encode-compare/
#2
1
You need to encode sal in request.send("sal="+sal). You'll probably find that if sal was equal to "foo & bar", you would end up with just "foo" on the server since the & needs to be encoded too. So:
您需要在request.send(“sal=”+sal)中对sal进行编码。您可能会发现,如果sal等于“foo & bar”,那么服务器上就只剩下“foo”,因为&也需要编码。所以:
request.send("sal=" + encodeURIComponent(sal)); // updated example
However, rather than doing this all by hand, you should think about using a library to do it for you, such as jQuery, then your example would look something like this:
然而,与其手工完成这些工作,不如考虑使用一个库来完成,比如jQuery,那么您的示例应该是这样的:
$.ajax({
url: "updateSal.php",
type: "POST",
data: { sal : $("salary").val() },
success: function(){
alert("update");
}
});