dtujfmfs06058 2019-06-10 12:08
浏览 163
已采纳

我怎样才能将php登录代码转换为javascript?

here is the code

<?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);

?>

i got it using Curl-to-PHP

by converting this command to php

curl "http://page/" --data "frashnum=&action=login&Frm_Logintoken=24&Username=admin&Password=admin"

and i need to get the value of the login token using this XMLHttpRequest request and js code first

<!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>

so i need to replace the Frm_Logintoken value with the console.log(str.match(pattern)1); result

what i have tried and failed

<!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>

it give me this error

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

And i don't know how to define the match console.log(str.match(pattern)[1]); results out of the function in the first js code

and there is something that i am Not sure about do i need the login token just like the php code or i can just insert the data then use the function function dosubmit() that's in the code of the page we are sending the data to !

  • 写回答

1条回答 默认 最新

  • doulou0882 2019-06-10 12:38
    关注

    In order to make an HTTP POST request in JavaScript you can use something like this(as shown here Send POST data using XMLHttpRequest)

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?