我有这条AJAX,通过GET方法发送用户名和密码来验证登录凭据。我想更新此代码以使用POST方法,但我不知道从哪里开始或要更改什么。
我这样做的原因是发送到另一个页面的数据会很大,而GET不会发送所有数据。
这是我的代码:
function createObject()
{
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer")
{
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
request_type = new XMLHttpRequest();
}
return request_type;
}
var http = createObject();
var usr;
var psw;
function login()
{
usr = encodeURI(document.getElementById('username').value);
psw = encodeURI(document.getElementById('password').value);
http.open('get', 'login.php?user='+usr+'&psw='+psw);
http.onreadystatechange = loginReply;
http.send(null);
}
function loginReply()
{
if(http.readyState == 4)
{
var response = http.responseText;
if(response == 0)
{
alert('Login failed! Verify user and password');
}
else
{
alert('Welcome ' + usr);
document.forms["doSubmit"].elements["usr"].name = "usr";
document.forms["doSubmit"].elements["usr"].value = usr;
document.forms["doSubmit"].elements["pwd"].name = "pwd";
document.forms["doSubmit"].elements["pwd"].value = psw;
document.forms["doSubmit"].action = location.pathname + "user/";
document.forms["doSubmit"].submit();
}
}
}
此代码使用GET并在URL中发送参数并等待回复。由于尺寸,我想通过POST发送参数。
将发送的数据是<textarea name='taData' id='taData'></textarea>
答案 0 :(得分:1)
按如下所述更改代码行:
发件人:强>
http.open('get', 'login.php?user='+usr+'&psw='+psw);
要强>
http.open('post', 'login.php');
http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
http.send('user=' + usr + '&psw=' + psw + '&tboxName=' + yourTextBoxValue);
有关该主题的更多信息:
http://msdn.microsoft.com/en-us/library/windows/desktop/ms757849(v=vs.85).aspx