无法通过AJAX发送'+'?
我使用Ajax POST方法发送数据,但我无法发送'+'(运营商到服务器,即如果我想发送1 +或20k +它只会发送1或20k ..只是消灭' +') HTML代码都在这里..无法通过AJAX发送'+'?
<form method='post' onsubmit='return false;' action='#'>
<input type='input' name='salary' id='salary' />
<input type='submit' onclick='submitVal();' />
</form>
和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;
}
在功能submitVal
()它第一次警报的,因为它是(薪水值,如果1+然后警报1+),但是当它发布它只是发布的值不需要'+'操作符,这是需要的... 它是查询字符串的任何问题,因为PH P的后端代码是工作的罚款...
使用
request.send("sal="+encodeURIComponent(sal));
的+被解释为在服务器端的空间,所以你需要先编码字符串。
此处了解详情:
是啊!它工作,它采取+作为一个空间......谢谢Tatu Ulmanen – 2010-06-01 08:25:45
您需要在编码萨尔request.send( “SAL =” + SAL)。您可能会发现,如果sal等于“foo &”,那么您最终只会在服务器上显示“foo”,因为&也需要进行编码。所以:
request.send("sal=" + encodeURIComponent(sal)); // updated example
然而,而不是全部由手工做这个,你应该考虑使用一个库来为你做它,如jQuery,那么你的例子是这个样子:
$.ajax({
url: "updateSal.php",
type: "POST",
data: { sal : $("salary").val() },
success: function(){
alert("update");
}
});
'encodeURI()'不会编码'〜!@#$&*()=:/,;?+'',使用'encodeURIComponent )'而不是。 – 2010-06-01 08:23:12
我也尝试encodeURI,但它不工作... – 2010-06-01 08:28:48
干杯Tatu,更新。 – Douglas 2010-06-01 08:30:25
这个问题已经回答了http://stackoverflow.com/questions/1373414/ajax-post-and-plus-sign-how-to-encode – Arib 2010-06-01 08:25:49