小编典典
更新:请参阅下面的评论流,与jQuery无关,这是服务器上的文件权限问题。
原始答案 :
您是否从浏览器中得到任何错误?例如,在Chrome或Safari中,如果打开开发工具并查看“控制台”选项卡,是否显示错误?或者在Firefox中,安装Firebug并检入Firebug的控制台。或者在IE中,使用VS.Net的免费版本…某些事情应该向您抱怨。
您还可以通过提供error函数而不是假设成功来从代码本身获取更多信息:
jQuery.ajax({
async:false,
type:'GET',
url:script,
data:null,
success:callback,
dataType:'script',
error: function(xhr, textStatus, errorThrown) {
// Look at the `textStatus` and/or `errorThrown` properties.
}
});
更新 :您说过您看到textStatus=’错误’和errorThrown=未定义。很奇怪。是否 相同的
脚本工作,如果你移动它,所以它不是一个子路径?我想知道子路径是否是红色鲱鱼,而真正的问题是脚本中的语法错误。
离题 : 真的 需要同步吗?您不能只轮询显示一个符号吗?只是同步ajax请求 确实 浪费了用户体验。在许多浏览器中,不仅您自己的页面而且
所有 页面在请求期间 都被 锁定。
这就是轮询的意思:假设我想从JavaScript异步加载jQuery:
function loadScript(url, symbol, callback) {
var script, expire;
// Already there?
if (window[symbol]) {
setTimeout(function() {
callback('already loaded');
}, 0);
}
// Determine when to give up
expire = new Date().getTime() + 20000; // 20 seconds
// Load the script
script = document.createElement('script');
script.type = 'text/javascript';
script.src = url;
document.body.appendChild(script);
// Start looking for the symbol to appear, yielding as
// briefly as the browser will let us.
setTimeout(lookForSymbol, 0);
// Our symbol-checking function
function lookForSymbol() {
if (window[symbol]) {
// There's the symbol, we're done
callback('success');
}
else if (new Date().getTime() > expire) {
// Timed out, tell the callback
callback('timeout');
}
else {
// Schedule the next check
setTimeout(lookForSymbol, 100);
}
}
}
用法:
// Load jQuery:
loadScript("path/to/jquery.min.js", "jQuery", function(result) {
// Look at 'result'
});
2020-07-26