无法从javascript函数发送XmlHttpSreRequest
我有一个用于表单提交的javscript函数无法从javascript函数发送XmlHttpSreRequest,javascript,php,html,Javascript,Php,Html,我有一个用于表单提交的javscript函数 function TxEncrypt(event) { //perform encryption of token data, then submit the form like normal //obtain public key and initial JSEncrypt object var txPubKey = txJ$(".zwitch_encryptionkey").val(); var txEncrypter
function TxEncrypt(event)
{ //perform encryption of token data, then submit the form like normal
//obtain public key and initial JSEncrypt object
var txPubKey = txJ$(".zwitch_encryptionkey").val();
var txEncrypter = new JSEncrypt();
txEncrypter.setPublicKey(txPubKey);
//get Data and encrypt it
var txData = '{}';
var txCryptData = '';
if(txJ$(".zwitch_data").length > 1)
{ //if there are more than one element with this class, convert it to json string
txData = txJ$(".zwitch_data").serializeObject();
txCryptData = txEncrypter.encrypt(JSON.stringify(txData));
}
else
{ //else, just encrypt the value
txData = txJ$(".zwitch_data").val();
txCryptData = txEncrypter.encrypt(txData);
}
dataString = txCryptData; // array?
var cipher=dataString;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
result=xmlhttp.responseText;
alert(result);
}
}
xmlhttp.open("POST","tokenize.php?cipher="+cipher,true);
xmlhttp.send();
}
提交时,该函数工作正常。我可以警告密码值
完成POST的php是
$cipher = $_REQUEST['cipher'];
echo $cipher;
当我尝试单独使用xmlhttprequest部分时,它起了作用。但是当我将其放入函数中时,它就不起作用了。这里有什么问题吗?很抱歉,但是为什么你用
jquery
标记你的问题,却没有使用它有用的$.ajax
组件?防止提交表单的默认行为,然后在ajax的成功回调中提交它request@A.Wolff谢谢你。我是新来的。你能给我举个例子吗suggested@PiyaSharma@A.Wolff的意思是在函数TxEncrypt(event){之后执行此event.preventDefault()
。它将阻止表单的默认行为。