[英]how can i convert php login code to java script?
這是代碼
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://page/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "frashnum=&action=login&Frm_Logintoken=4&Username=admin&Password=admin");
curl_setopt($ch, CURLOPT_POST, 1);
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
我使用Curl-to-PHP得到它
通過將此命令轉換為php
curl "http://page/" --data "frashnum=&action=login&Frm_Logintoken=24&Username=admin&Password=admin"
我需要首先使用此XMLHttpRequest請求和js代碼獲取登錄令牌的值
<!DOCTYPE html>
<html>
<body>
<script>
httpRequest = new XMLHttpRequest()
httpRequest.onreadystatechange = function() {
if (httpRequest.readyState == XMLHttpRequest.DONE) {
let str =(httpRequest.responseText);
alert(str)
let pattern =/\bgetObj\("Frm_Logintoken"\)\.value = "([^"]+)";/;
console.log(str.match(pattern)[1]);
}
}
httpRequest.open('GET', 'http://page/', true);
httpRequest.send(null);
</script>
</body>
</html>
所以我需要用console.log替換Frm_Logintoken值(str.match(pattern) 1 ); 結果
我嘗試過但失敗了
<!DOCTYPE html>
<html>
<body>
<script>
var xhr = new XMLHttpRequest();
xhr.open('post','http://page/', true);
xhr.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.onload = function () {
// do something to response
console.log(this.responseText);
};
xhr.send('document.getElementById("Frm_Username").value = "test"');
xhr.send('document.getElementById("Frm_Password").value = "test"');
xhr.send('function dosubmit()');
</script>
</body>
</html>
它給了我這個錯誤
未捕獲的DOMException:無法在'XMLHttpRequest'上執行'send':對象的狀態必須為OPENED。
我不知道如何定義匹配console.log(str.match(pattern)[1]);
結果出了第一個js代碼中的函數
並且有一些東西,我不確定我是否需要登錄令牌就像PHP代碼或我可以只是插入數據然后使用函數function dosubmit()
在我們發送數據的頁面的代碼中!
為了在JavaScript中發出HTTP POST請求,您可以使用類似這樣的內容(如此處所示使用XMLHttpRequest發送POST數據 )
var http = new XMLHttpRequest();
var url = 'http://page/';
var params = 'frashnum=&action=login&Frm_Logintoken=4&Username=admin&Password=admin';
http.open('POST', url, true);
//Send the proper header information along with the request
http.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
http.send(params);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.