-2

I have one server side js file. I need to take whole source from that file and show it. I have the url i.e. location of the js file in the server. when I hit the url in browser then I am getting response and I can see that source. But when I am trying this code, it is not working. no response, even can't see the alert msg(mean alert("1")), and just stop. please solve this problem.

            $.get("http://localhost:8080/web/js/serviceFF.js", function(file) {
              alert("1");
              $("textarea").val(file);
            });
javaboy
  • 99
  • 7

1 Answers1

0

The source file that you are running - is it also being viewed from http://localhost:8080 too? If it isn't, you may be running into XSS (cross-site script execution) browser protection.

So in other words, the code listed above should be on http://localhost:8080/testpage.html and you need to run it from there.

If it is on your local computer, like the browser showing file://home/user/Desktop/testpage.html - the browser may block it to prevent XSS exploits.

Also, same origin policy comes into play too. Please refer to the wiki table:

https://en.wikipedia.org/wiki/Same-origin_policy

PressingOnAlways
  • 11,948
  • 6
  • 32
  • 59
  • Isn't adding localhost links in answers a bad idea? It will work only on your machine. – Obscure Geek Jul 16 '15 at 04:27
  • but if I try to get html file as same as that way, it works! it is not possible to get whole source of js file? – javaboy Jul 16 '15 at 04:28
  • sounds like XSS protection. They allow HTML file because there is nothing that can be executed that can hijack the browser session. JS on the other hand can be executed and run malicious code. Refer to this stack overflow post on how to make scripts from another domain work: http://stackoverflow.com/questions/5351236/javascript-cross-domain-allow-other-domains – PressingOnAlways Jul 16 '15 at 04:45
  • @ObscureGeek - the point is that he has to run the script from the same place he is trying to get the js file from. In his example - "http://localhost:8080/web/js/serviceFF.js" hence, I used his example. – PressingOnAlways Jul 16 '15 at 04:46