haris
haris

Reputation: 75

javascript in perl code

i using javascript file (json2.js) in perl-cgi code with apache. but when i run it on browser, its unable to find the source and return following error; 'Failed to load resource: the server responded with a status of 404 (Not Found)'

my doumentroot path is : /srv/www/cgi-bin and scripts' path: /srv/www/cgi-bin/scripts

i doing this;

 print "<script type='text/javascript' src='./scripts/json2.js'></script>"; # Line 1
    print "<script type='text/javascript' language='JavaScript'>
    function setDetails(o,json_arrRef,arrSize){
            alert('hello='+arrSize+' || ref='+json_arrRef); // till here it works fine
            var json_obj = JSON.parse(json_arrRef);     
    }
    </script>";

if I edit Line 1 as

print "<script type='javascript' src='./scripts/json2.js'></script>";

it finds the source but gives following error when 'JSON.parse()' is called; 'Uncaught SyntaxError: Unexpected token ILLEGAL'

am I doing something wrong?

Upvotes: 0

Views: 372

Answers (2)

Alexandr Ciornii
Alexandr Ciornii

Reputation: 7390

Replace "./scripts/json2.js" with "/scripts/json2.js".

Upvotes: 0

Quentin
Quentin

Reputation: 944441

  • Don't put static files anywhere in or below /cgi-bin/ as servers are often configured to treat that path is a special way
  • Do get your type attributes correct, the content-type for JavaScript is text/javascript and not just javascript (technically speaking it should be application/javascript, but pretend it is text/javascript for the sake of browsers)
  • Don't compare JavaScript errors in the browser with Perl code and do look at what the webserver is outputting (i.e. the HTML and JavaScript source and/or which requests give 404 or other HTTP error codes)

Upvotes: 2

Related Questions