banri16 banri16 - 4 years ago 271
Javascript Question

OpenCart throws JS error 'Cannot read property "document" of null' when hosted

When I'm trying opencart on my local, it seems that it's working fine. But when I uploaded to the hosting/server, some errors appear, please see below:

Uncaught TypeError: Cannot read property 'document' of null
f.each.contents @ jquery-1.7.1.min.js:3 @ jquery-1.7.1.min.js:2
f.fn.(anonymous function) @ jquery-1.7.1.min.js:3
(anonymous function) @ crossdomain.php?session_id=66e844dc45af19ce293981cdd9b22b67:566
n @ jquery-1.7.1.min.js:2 o.fireWith @ jquery-1.7.1.min.js:2
e.extend.ready @ jquery-1.7.1.min.js:2
c.addEventListener.B @ jquery-1.7.1.min.js:2

What seems to be the problem? Because in my local, these errors doens't appear. Thanks.

Answer Source

One big difference between running hosted and running locally is the security treatment of network calls. The reference to crossdomain.php in your stack trace suggests that you're running into this.

If so, then there are two possible solutions:

If you're running in Flash: You need to host a crossdomain.xml file at the root of the remote domain that grants permission to the local domain (or, as in this example, everyone):

<?xml version="1.0"?>
<!DOCTYPE cross-domain-policy SYSTEM "">
   <allow-access-from domain="*" />

This file must always be at the root of the remote domain, e.g., .

If Flash is not involved, then CORS is the security mechanism responsible for blocking this call, and you need to configure the remote web server to add an Access-Control-Allow-Origin header to the response to the URL you're reaching out to.

Access-Control-Allow-Origin: *
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download