mockjs step on the pit
Posted May 26, 2020 • 1 min read
I encountered a problem today. When loading the model with threejs' FbxLoader, if the mock is turned on, the model cannot be loaded, so let's see what the hell is.
After the introduction of Mock, a mockXMLHttpRequest will be created inside mock.js to replace the native XMLHttpRequest. If there is a native XHR request, it will be intercepted, replaced with the MockXMLHttpRequest object, and then match the corresponding url. If not, send a network request .
FbxLoader is a FileLoader object inherited from Three.js. When calling the load method, it will first determine whether the incoming url is a DataURI in Base64 format. If it is, it will be parsed and returned. If not, it will create an XHR request.
Where is the problem?
Debugged into FbxLoader and found that after the load is successful, the return value is buffer, which needs to be parsed, and the responseType needs to be of type 'arrayBuffer'! The problem is coming. In MockXMLHTTP of mockjs, the responseType is not set, but is set to a null value, so the buffer object cannot be parsed.