XMLHttpRequest cannot load - Chrome

Hey I’ve been trying to get asset bundles to load and after much trial and error and playing with htaccess files I got Firefox to do it! However Chrome is throwing an error :frowning:

Here is the WebGL app:
http://getinspir3d.com/inspir3d-viewer6/index.html

I am unsure of if my htacess file(s) are set up correctly. Please let me know what changes I need to do if any.

Here is my htaccess file for the above page:

<IfModule mod_headers.c>
    SetEnvIf Origin (.*) AccessControlAllowOrigin=$1
    Header add Access-Control-Allow-Origin %{AccessControlAllowOrigin}e env=AccessControlAllowOrigin
    Header set Access-Control-Allow-Credentials true
</IfModule>

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP:Accept-encoding} gzip
RewriteRule (.*)Release(.*)\.js $1Compressed$2\.jsgz [L]
RewriteRule (.*)Release(.*)\.data $1Compressed$2\.datagz [L]
RewriteRule (.*)Release(.*)\.mem $1Compressed$2\.memgz [L]
RewriteRule (.*)Release(.*)\.unity3d $1Compressed$2\.unity3dgz [L]
AddEncoding gzip .jsgz
AddEncoding gzip .datagz
AddEncoding gzip .memgz
AddEncoding gzip .unity3dgz

I couldn’t get the asset bundle to load from the same folder as the app. So it’s located here:
http://getinspir3d.com/inspir3d-viewer4/Patmos.unity3d

Here is the htaccess file from where the asset bundle is stored:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP:Accept-encoding} gzip
RewriteRule (.*)Release(.*)\.js $1Compressed$2\.jsgz [L]
RewriteRule (.*)Release(.*)\.data $1Compressed$2\.datagz [L]
RewriteRule (.*)Release(.*)\.mem $1Compressed$2\.memgz [L]
RewriteRule (.*)Release(.*)\.unity3d $1Compressed$2\.unity3dgz [L]
AddEncoding gzip .jsgz
AddEncoding gzip .datagz
AddEncoding gzip .memgz
AddEncoding gzip .unity3dgz

<IfModule mod_headers.c>
    Header always set "Access-Control-Allow-Credentials": "true",
    Header always set "Access-Control-Allow-Headers": "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time",
    Header always set "Access-Control-Allow-Methods": "GET, POST, OPTIONS",
    Header always set "Access-Control-Allow-Origin": "*",
</IfModule>

This is the error I’m getting in Chrome:

GET http://www.getinspir3d.com/inspir3d-viewer4/Patmos.unity3d _JS_WebRequest_Send @ profile33.js:1fmg @ profile33.js:18ivc @ profile33.js:21Wge @ profile33.js:19TVl @ profile33.js:32invoke_viiii @ profile33.js:1hpk @ profile33.js:5UVl @ profile33.js:32invoke_viiiii @ profile33.js:1gpk @ profile33.js:5CWl @ profile33.js:32invoke_viii @ profile33.js:1Pyg @ profile33.js:18vFk @ profile33.js:5$Wl @ profile33.js:32invoke_iiii @ profile33.js:1XP @ profile33.js:28Lu @ profile33.js:28aQc @ profile33.js:21vId @ profile33.js:13tId @ profile33.js:13EFd @ profile33.js:20tGd @ profile33.js:20AGd @ profile33.js:20vGd @ profile33.js:20uGd @ profile33.js:20Bie @ profile33.js:19$Wl @ profile33.js:32invoke_iiii @ profile33.js:1Brk @ profile33.js:5rVl @ profile33.js:32invoke_iiiii @ profile33.js:1Wyg @ profile33.js:18wQk @ profile33.js:7$Wl @ profile33.js:32invoke_iiii @ profile33.js:1XP @ profile33.js:28Lu @ profile33.js:28aQc @ profile33.js:21wId @ profile33.js:13yGd @ profile33.js:20AGd @ profile33.js:20KJd @ profile33.js:13ang @ profile33.js:18(anonymous function) @ VM157:1SendMessage @ profile33.js:1OnLoaded @ index.html:11(anonymous function) @ VM156:1_JS_Eval_EvalJS @ profile33.js:1ohe @ profile33.js:19KWl @ profile33.js:32invoke_vi @ profile33.js:1dqk @ profile33.js:5CWl @ profile33.js:32invoke_viii @ profile33.js:1bqk @ profile33.js:5TVl @ profile33.js:32invoke_viiii @ profile33.js:1Vyg @ profile33.js:18uFk @ profile33.js:5$Wl @ profile33.js:32invoke_iiii @ profile33.js:1XP @ profile33.js:28Lu @ profile33.js:28aQc @ profile33.js:21vId @ profile33.js:13yGd @ profile33.js:20AGd @ profile33.js:20hGd @ profile33.js:20WIc @ profile33.js:21ind @ profile33.js:20cmg @ profile33.js:18EWl @ profile33.js:32Runtime.dynCall @ profile33.js:1(anonymous function) @ profile33.js:1Browser.mainLoop.runIter @ profile33.js:1Browser_mainLoop_runner @ profile33.js:1
index.html:1 XMLHttpRequest cannot load http://www.getinspir3d.com/inspir3d-viewer4/Patmos.unity3d. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://getinspir3d.com' is therefore not allowed access. The response had HTTP status code 500.
profile33.js:1 NullReferenceException: A null value was found where an object instance was required.
LoadBundle+<DownloadBundle>c__Iterator1.MoveNext ()

This is the script I wrote to load the asset bundle and instantiate the prefab inside of it:

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class LoadBundle : MonoBehaviour {

    public Transform parent;

    public GameObject loadingUI;

    public RectTransform progressBar;
    Vector2 progressStart;

    Material mat;

    void Start()
    {
        mat = gameObject.GetComponent<Renderer>().material;
        progressStart = progressBar.sizeDelta;
        progressBar.sizeDelta = new Vector2(0f, progressStart.y);
        Application.ExternalCall("OnLoaded");
    }

    public void Load(string url)
    {

        StartCoroutine("DownloadBundle", url);

    }

    IEnumerator DownloadBundle(string url)
    {
        Debug.Log("Loading = " + url);
        WWW www = new WWW(url);

        while (!www.isDone)
        {
            Debug.Log("Progress = " + www.progress * 100f);
            UpdateProgress(www.progress);
            yield return null;
        }

        AssetBundle bundle = www.assetBundle;
        string prefabName = "PrefabNotSet";
        bundle.LoadAllAssets();
        foreach(string bundleName in bundle.GetAllAssetNames())
        {
            if (bundleName.Contains(".prefab"))
            {
                prefabName = bundleName;
            }
            //Debug.Log("checked " + bundleName);
        }

        Debug.Log("Before " + prefabName);
        //searches for slash index and gets name of prefab
        int slashIndex = 0;
        for (int i = 0; i < prefabName.Length; i++)
        {
            if (prefabName[i].Equals('/')) { slashIndex = i; }
        }

        int nameLength = prefabName.Length - slashIndex - 1;
        prefabName = prefabName.Substring(slashIndex + 1, nameLength);

        Debug.Log("After " + prefabName);

        GameObject instance = Instantiate(bundle.LoadAsset(prefabName)) as GameObject;
        //GameObject instance = Instantiate(bundle.LoadAsset("patmos" + ".prefab")) as GameObject;

        instance.transform.parent = parent;
        instance.transform.position = Vector3.zero;
        instance.transform.eulerAngles = Vector3.zero;
        instance.transform.localScale = Vector3.one;

        yield return null;

        bundle.Unload(false);

        Roof.Instance.SetRoof();
        loadingUI.SetActive(false);

    }

    void UpdateProgress(float percent)
    {
        progressBar.sizeDelta = new Vector2(percent * progressStart.x, progressStart.y);
    }
}

I’ve been doing some searching and I think this has to do with CORS.

My webhost is siteground.com and I have the most basic shared hosting package.

Anyone run into this problem before? Any suggestions are welcome I’d love some help :slight_smile:

Thanks!
-Ben

I ran across this page of documentation.

I wasn’t sure where the “Access-Control” lines of code were supposed to go. Does anyone know?

I believe the CORS headers need to be set at the root of the site (getinspir3d.com in this case). They’re only needed on the server. Right now, however, Im getting HTTP 500 errors trying to access the Patmos.unity3d URL so something is breaking on the server during the request.

I’ve been messing with the htaccess files a bit which is why you got a HTTP 500 error. I realized why I was able to get it working in Firefox but not Chrome.

For some reason the asset bundle downloads for 5 seconds and then stops. Firefox caches that 5 seconds of downloading. Chrome does not. So after 2-3 page refreshes Firefox will finish loading the asset bundle. But Chrome will start over from the beginning each refresh. (i changed the extension of the asset bundle from .unity3d to .jpg for testing purposes)

Chrome stops at exactly 5 seconds every time:

I added "Header always set Access-Control-Max-Age “7000"” to my htaccess file thinking maybe it will allow for longer downloads, but it didn’t seem to do anything.

Thanks for the tip sirrus. I already had an htaccess file in the root of my site, so I copied and pasted in the headers:

This is the root htaccess file:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
AddType application/octet-stream mem data jsgz datagz memgz
AddType application/octet-stream .mem .data .jsgz .datagz .memgz
AddType application/octet-stream unity3dgz .unity3dgz
AddType application/octet-stream .unity3d unity3d

<IfModule mod_headers.c>
    Header always set Access-Control-Max-Age "7000"
    Header always set Access-Control-Allow-Credentials "true"
    Header always set Access-Control-Allow-Headers "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time"
    Header always set Access-Control-Allow-Methods "GET, POST, OPTIONS"
    Header always set Access-Control-Allow-Origin "*"
</IfModule>

Here is the latest htaccess file which is in the same directory as the webgl app:

<IfModule mod_headers.c>
    Header always set Access-Control-Max-Age "7000"
    Header always set Access-Control-Allow-Credentials "true"
    Header always set Access-Control-Allow-Headers "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time"
    Header always set Access-Control-Allow-Methods "GET, POST, OPTIONS"
    Header always set Access-Control-Allow-Origin "*"
</IfModule>

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP:Accept-encoding} gzip
RewriteRule (.*)Release(.*)\.js $1Compressed$2\.jsgz [L]
RewriteRule (.*)Release(.*)\.data $1Compressed$2\.datagz [L]
RewriteRule (.*)Release(.*)\.mem $1Compressed$2\.memgz [L]
RewriteRule (.*)Release(.*)\.unity3d $1Compressed$2\.unity3dgz [L]
AddEncoding gzip .jsgz
AddEncoding gzip .datagz
AddEncoding gzip .memgz
AddEncoding gzip .unity3dgz

Does anyone know why an asset bundle would only download for 5 seconds? I feel like I am getting close to getting this working :slight_smile:

Thanks!
-Ben

I did some googling for 5 second timeouts and ran across this:
https://httpd.apache.org/docs/2.4/mod/core.html#keepalivetimeout

Since I have shared hosting I don’t have access to the httpd.conf file, but I found a work around using the htaccess file.

Here is my lastest htaccess file:

<ifModule mod_env.c>
  SetEnv KeepAlive On
  SetEnv KeepAliveTimeout 100
  SetEnv MaxKeepAliveRequests 500
</ifModule>

<IfModule mod_headers.c>
    Header unset Connection
    Header set Connection keep-alive

    Header unset Keep-Alive
    Header set Keep-Alive timeout=100,max=500
 
    Header always set Access-Control-Max-Age "7000"
    Header always set Access-Control-Allow-Credentials "true"
    Header always set Access-Control-Allow-Headers "Accept, X-Access-Token, X-Application-Name, X-Request-Sent-Time"
    Header always set Access-Control-Allow-Methods "GET, POST, OPTIONS"
    Header always set Access-Control-Allow-Origin "*"
   
</IfModule>

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP:Accept-encoding} gzip
RewriteRule (.*)Release(.*)\.js $1Compressed$2\.jsgz [L]
RewriteRule (.*)Release(.*)\.data $1Compressed$2\.datagz [L]
RewriteRule (.*)Release(.*)\.mem $1Compressed$2\.memgz [L]
RewriteRule (.*)Release(.*)\.unity3d $1Compressed$2\.unity3dgz [L]
AddEncoding gzip .jsgz
AddEncoding gzip .datagz
AddEncoding gzip .memgz
AddEncoding gzip .unity3dgz

Unfortunately it didn’t seem to change anything. It still gets stuck at 5 seconds :frowning:

WAIT A MINUTE!!!

I"M USING 5.1.0f2!!! BRB Updating Unity :smile:

YES!!! It’s working!!!
http://getinspir3d.com/inspir3d-viewer7/index.html

Updating to 5.2.2f1 did the trick!

I’m just using the standard htaccess file:

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{HTTP:Accept-encoding} gzip
RewriteRule (.*)Release(.*)\.js $1Compressed$2\.jsgz [L]
RewriteRule (.*)Release(.*)\.data $1Compressed$2\.datagz [L]
RewriteRule (.*)Release(.*)\.mem $1Compressed$2\.memgz [L]
RewriteRule (.*)Release(.*)\.unity3d $1Compressed$2\.unity3dgz [L]
AddEncoding gzip .jsgz
AddEncoding gzip .datagz
AddEncoding gzip .memgz
AddEncoding gzip .unity3dgz

One thing that is is annoying is my asset bundle loading bar isn’t working anymore. It goes from 0 to loaded. I think this might be a bug with the WWW class.

 WWW www = new WWW(url);

        while (!www.isDone)
        {
            Debug.Log("Progress = " + www.progress * 100f);
            UpdateProgress(www.progress);
            yield return null;
        }


Debug.Log("Progress = " + www.progress * 100f); is being called many times but still is printing out 0, but the model loads. I am testing this in incognito mode so it shouldn’t be cached. I’ll file a bug report.

Thanks for the help sirrus :slight_smile:

So excited right now :smile:

Glad you got it working. That’s always a great feeling :slight_smile: