问题
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"> Email : </label>
<input type="text" id="user" /><span id="semail"></span>
<br><br>
<label for="pass">Password : </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