我有邏輯,我試圖動(dòng)態(tài)添加主機(jī) url,因此它可以在所有基于主機(jī)的 env 中工作,因此下面嘗試查找主機(jī)中存在的文件,但它永遠(yuǎn)不會(huì)進(jìn)入$.each語句,如果直接調(diào)用 url,http://18.35.168.87:6000/Sdk/wrapper-sdk/client/out.json它可以工作并呈現(xiàn)數(shù)據(jù),知道在下面的代碼中可能有什么問題來完成這項(xiàng)任務(wù)嗎?主文件function myFunction(val) { var url = "../../wrapper-sdk/" + client + "/out.json"; if (window.location.hostname.indexOf("localhost") !== -1 || window.location.host.indexOf("localhost") !== -1) { var scripts = document.getElementsByTagName('script'); var host = ''; $.each(scripts, function (idx, item) { if (item.src.indexOf('Sdk/wrapper-sdk') !== -1 && (item.src.indexOf('out.json') !== -1)) { host = item.src.split('?')[0]; host = host.replace('wrapper-sdk/' + client + '/out.json', ''); } }); url = url.replace('../../', host); } $.ajax({ url: url + "?no_cache=" + new Date().getTime(), dataType: "json", "async": false, success: function (data) { console.log(data); }, error: function () { console.log('error while accessing api.json.') } });}
如何使用Jquery動(dòng)態(tài)添加屬性值?
藍(lán)山帝景
2021-10-21 15:12:20