我尝试使用以下代码在HTML中测试我的XHR:
<html>
<head>
<title> testing </title>
</head>
<body>
<div>
<script lang="JavaScript">
var clientID = "abc";
var secret = "def";
var oReq = new XMLHttpRequest();
oReq.withCredentials = true;
oReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
document.write("Going to make a call");
//OAuthAccessor.clientID, OAuthAccessor.secret
oReq.onreadystatechange = function () {
//console.log
document.write("state changed - new state: " + oReq.readyState + " and Status: " + oReq.status);
if (oReq.readyState === 4) {
if (oReq.status === 200) {
//console.log
document.write(oReq.responseText);
} else {
//console.log
document.write("Error: " + oReq.status + " Message: " + oReq.responseText);
}
}
};
//console.log
document.write("Status: " + oReq.status);
oReq.open("POST", "https://api.sandbox.paypal.com/v1/oauth2/token", true, clientID, secret);
oReq.send();
//console.log
document.write("Request send");
</script>
</div>
</body>
</html>
我没有在我的文档上写下任何内容 - 该代码有什么问题?从仿真器执行时,它可以工作。
答案 0 :(得分:1)
未捕获DOMException:无法在'XMLHttpRequest'上执行'setRequestHeader':对象的状态必须为OPENED。
更改代码的顺序,使其显示为:
var oReq = new XMLHttpRequest();
oReq.open("POST", "https://api.sandbox.paypal.com/v1/oauth2/token", true, clientID, secret);
...解决了这个问题。
始终阅读您的错误消息。