admin管理员组文章数量:1416307
I'm trying to load a javascript file locally with getScript, but it returns a 404 error.
test.js - the script I'm trying to load:
var hello = function(){
alert( "test" );
}
hello();
jQuery code from index.html:
<script type='text/javascript'>
$.getScript( "js/test.js" )
.fail( function( e ){ $( 'body' ).html( JSON.stringify( e ) ) } )
.done( function(){ alert( "Success" ) } );
</script>
When I open index.html in Chrome, getScript fails, and the following is displayed in the body:
{"readyState":4,"responseText":"","status":404,"statusText":"error"}
So obviously jQuery can't find the file specified. But the weird thing is, when I use the traditional tags:
<script type='text/javascript' src='js/test.js'></script>
it works fine.
Also, getScript works fine if I'm loading from an external site, such as:
$.getScript( "" )
So what could be the problem?
I'm trying to load a javascript file locally with getScript, but it returns a 404 error.
test.js - the script I'm trying to load:
var hello = function(){
alert( "test" );
}
hello();
jQuery code from index.html:
<script type='text/javascript'>
$.getScript( "js/test.js" )
.fail( function( e ){ $( 'body' ).html( JSON.stringify( e ) ) } )
.done( function(){ alert( "Success" ) } );
</script>
When I open index.html in Chrome, getScript fails, and the following is displayed in the body:
{"readyState":4,"responseText":"","status":404,"statusText":"error"}
So obviously jQuery can't find the file specified. But the weird thing is, when I use the traditional tags:
<script type='text/javascript' src='js/test.js'></script>
it works fine.
Also, getScript works fine if I'm loading from an external site, such as:
$.getScript( "http://www.youtube./player_api" )
So what could be the problem?
Share Improve this question asked Nov 10, 2014 at 19:13 j.sorelj.sorel 231 silver badge3 bronze badges 3- 2 So look at the path, what is wrong with it. What should it be? Issue is not with getScript, the problem is with where the file is located. – epascarello Commented Nov 10, 2014 at 19:14
- Try using the full path starting with http:// and including the domain as a test so you can figure out why the path isn't working properly. – jfriend00 Commented Nov 10, 2014 at 19:15
- @jfriend00 I'm running this on my local machine if that matters. I've tried passing the url as both "/home/sorel/projects/test/js/test.js" and "file:///home/sorel/projects/test/js/test.js" and neither work. The latter opens in my browser. And again, using a <script> tag with src as the latter absolute path works as well, so I don't think it is a problem with the path I'm specifying. – j.sorel Commented Nov 10, 2014 at 19:49
1 Answer
Reset to default 4Ahhh, the key is that you're running this on your local machine and trying to fetch a script from your local hard drive.
The issue is that $.getScript()
uses an Ajax call to load the script and Ajax calls are not allowed by default to fetch contents from your hard drive in some browsers (like Chrome) for security reasons. This would not be an issue if loading from a real web server.
You can work around it by inserting a script tag yourself instead of using jQuery's $.getScript()
or by not trying to run this from your local hard drive. There is also a Chrome mand line parameter that will bypass this security restriction which you could use for development purposes (not remended for general purpose browsing).
本文标签: javascriptgetScript not loading local scriptStack Overflow
版权声明:本文标题:javascript - getScript not loading local script - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745244883a2649509.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论