我是javascript和HTML的新手。我收到此错误
“未捕获的ReferenceError:未定义require”
当我尝试用HTML调用javascript文件时。我尝试了在不同门户网站中建议的各种方法,但未成功。我在nodeJS中执行此操作,而我使用的编辑器是VS Code。独立代码可以正常工作。非常感谢您的工作。
注意:可能很愚蠢-我已将require.js和request.js添加到工作文件夹中:)
JavaScript文件
var request = require('request')
function handleTokenCallback(body){
var edgeOptions = {
url: 'https://api.mypurecloud.de/api/v2/telephony/providers/edges',
headers: {
'Authorization': body.token_type + " " + body.access_token
}
};
request(edgeOptions, function (error, response, body) {
if (!error && response.statusCode == 200) {
var EdgeJson=JSON.parse(JSON.stringify(JSON.parse(body), null, 2))
console.log("\n" + '*******************EDGE STATUS******************' + "\n")
for(i=0;i<=2;i++)
{
console.log('Name: ' + EdgeJson.entities[i].name)
console.log('Online Status: ' + EdgeJson.entities[i].onlineStatus)
console.log('State: ' + EdgeJson.entities[i].state)
console.log('Health Status: ' + EdgeJson.entities[i].healthStatus)
console.log('Call Drain Status: ' + EdgeJson.entities[i].callDrainingState)
console.log('Edge Version: ' + EdgeJson.entities[i].softwareStatus.version.edgeVersion +
"\n")
}
}else{
console.log('Printing Error: '+ error)
console.log(body.token_type + ' ' + body.access_token)
}
});
}
secret="*********";
id="*********";
request.post({url:'https://login.mypurecloud.de/oauth/token', form:
{grant_type:'client_credentials'}}, function(err,httpResponse,body){
if(err == null){
handleTokenCallback(JSON.parse(body));
//console.log(body)
}
}).auth(id,secret,true)
HTML文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script src="*the js file above*">
//Basically I want to print all the "console.log" items seen in the javascript file.
</script>
</body>
</html>