0

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?

2 Answers 2

2
  • 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)
Sign up to request clarification or add additional context in comments.

3 Comments

thanks David, 1. I put the script files on the document root but the result is same. 2. I already tried with these option (application/javascript and text/javascript) but no effects. 3. http 404 was due to Javascript file for sure
ok instead of using JSON.parse() I tried with eval and it is working fine. i downloaded the json2.js from json.org and i don't know why it has a problem. but anyway thanks David
That would be because your JSON is invalid (JSON being a tiny subset of JavaScript). Test it with jsonlint.com
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.