如何将 php 登录代码转换为 java 脚本?

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>

所以我需要将 Frm_Logintoken 值替换为 console.log(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>

它给我这个错误

Uncaught DOMException: Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.

而且我不知道如何定义匹配console.log(str.match(pattern)[1]); 第一个js代码中的函数结果

有些事情我不确定我是否需要像 php 代码一样的登录令牌,或者我可以只插入数据然后使用代码中的函数 function dosubmit()我们将数据发送到的页面的!

为了在 JavaScript 中发出 HTTP 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);