How to use XHR in pop up window

天涯浪子 提交于 2019-12-12 01:29:28

问题


Hi I need to know how to read a API from pop up window. Here is my popup window

<!DOCTYPE html>
 <html>
<head>
    <script type="text/javascript" src="test.js"></script>       
</head>
<body>
<b>Enter your Email ID and Password</b><br><br>
    <form id="userinfo">
        <label for="user">&nbsp;Email&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
        <input type="text" id="user" /><span id="semail"></span>
        <br><br>
       <label for="pass">Password&nbsp;:&nbsp;&nbsp;&nbsp;&nbsp;</label>
       <input type="password" id="pass" />
        <br>
        <br>
        <input type="button" id="login" value="Log In"/>
    </form>
   </body>
</html>

Here is my test.js

window.addEventListener('DOMContentLoaded', function() {
var user  = document.querySelector('input#user');
 var pwd  = document.querySelector('input#pass');
var login = document.querySelector('input#login');

 login.addEventListener('click', function() {     

    var userStr = user.value; 
    var pwdStr = pwd.value;

    var req = getHTTPObject();;
    var url="http://blog.calpinetech.com/test/index.php";
    req.open("GET", url);
    req.send(null);
    if (req.status == 200) {        
       var item=req.responseText;
       alert(item);
    }
    window.close();

     chrome.runtime.getBackgroundPage(function(bgPage) {
            bgPage.updateIcon();
        });

      }); 

   });

Here when I click on the log in button it has to read the API(server file named "http://blog.calpinetech.com/test/index.php"). How can I do it?Please help me


回答1:


Btw, Any reason of not using XMLHttpRequest to make AJAX calls ?

Sample in your event listener could be helpful:

var userStr = user.value, 
    pwdStr = pwd.value,
    url="http://blog.calpinetech.com/test/index.php";
if (window.XMLHttpRequest) {
    xmlHttpReq = new XMLHttpRequest();
}
else if (window.ActiveXObject) {
    xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttpReq.open('POST', url, true);
xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xmlHttpReq.send("username="+userStr+"&passwd="+pwdStr);

/** Handle your response here */


来源:https://stackoverflow.com/questions/20349635/how-to-use-xhr-in-pop-up-window

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!