如何在弹出窗口中使用XHR [英] How to use XHR in pop up window

查看:129
本文介绍了如何在弹出窗口中使用XHR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我需要知道如何从弹出窗口读取API。
这是我的弹出窗口

 <!DOCTYPE html> 
< html>
< head>
< script type =text / javascriptsrc =test.js>< / script>
< / head>
< body>
< b>输入您的电子邮件ID和密码< / b>< br>< br>
< form id =userinfo>
< label for =user>& nbsp;电子邮件& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp;& nbsp ;&安培; NBSP;&安培; NBSP;< /标签>
< input type =textid =user/>< span id =semail>< / span>
< br>< br>
< label for =pass>密码& nbsp;:& nbsp;& nbsp;& nbsp;& nbsp;< / label>
< input type =passwordid =pass/>
< br>
< br>
< input type =buttonid =loginvalue =登录/>
< / form>
< / body>
< / html>

这是我的test.js

 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();
});

});

});

在这里,当我点击登录按钮时,它必须读取API(服务器文件名为 a href =http://blog.calpinetech.com/test/index.php =nofollow> http://blog.calpinetech.com/test/index.php )。我应该怎么做?请帮助我

解决方案

btw,任何不使用XMLHttpRequest进行AJAX调用的原因?



您的事件侦听器中的示例可能会有所帮助:

  var userStr = user。值,
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);

/ **在这里处理您的回复* /


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

解决方案

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 */

这篇关于如何在弹出窗口中使用XHR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆