我有一个JS文件,其中定义了一个名为WebRequest的const对象,我想在一个单独的JS文件中使用此对象,该文件在包含WebRequest对象的文件之后加载。我得到
ReferenceError:未定义WebRequest
Identity.Index.js 文件中的
。HTML :
<script crossorigin="anonymous" integrity="sha256-WpOohJOqMqqyKL9FccASB9O0KwACQJpFTUBLTYOVvVU=" src="https://code.jquery.com/jquery-3.4.1.js" type="application/JAVASCRIPT"></script>
<script src="/SCRIPT/WebRequest.js" type="application/JAVASCRIPT"></script>
<script src="/SCRIPT/Identity.Index.js" type="application/JAVASCRIPT"></script>
WebRequest.js :
$(document).ready(() => {
class RequestResult {
constructor(response) {
this.resp = response;
}
response() {
return this.resp;
}
success() {
return this.resp.ok;
}
status() {
return this.resp.status;
}
statusText() {
return this.resp.statusText;
}
}
const WebRequest = async (controller = "API", action, query = {}, data = {}, method = RequestMethods.POST, mode = RequestModes.SameOrigin, creds = RequestCredentials.SameOrigin, headers = {
"Content-Type": "application/json",
"Accept": "application/json"
}) => {
...
};
});
Identity.Index.js :
$(document).ready(() => {
var _searchBox = $("#searchBox");
var _searchTimeout = null;
_searchBox.keyup(() => {
clearTimeout(_searchTimeout);
_searchTimeout = setTimeout(doSearch, 500);
});
$(".search-submit").click(doSearch);
$(".result-wrap > .close-wrap > .icon").click(() => {
$(".result-wrap").hide();
_searchBox.val("");
_searchBox.focus();
});
async function doSearch() {
var str = _searchBox.val();
if (str.length > 0) {
$(".search-no-found").hide();
$(".working").show();
$(".search-result").hide();
$(".site-wrap").hide();
var result = await WebRequest("Identity", "Search", null, { text: str });
}
else {
$(".result-wrap").hide();
}
}
});