{"record":{"id":"094174541225a431","repo":"NativeScript/NativeScript","slug":"failed-to-execute-send-on-xmlhttprequest-the","errorCode":null,"errorMessage":"Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.","messagePattern":"Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/core/xhr/index.ts","lineNumber":233,"sourceCode":"\t\t\tthis._errorFlag = true;\n\t\t\tthis._sendFlag = false;\n\t\t\tthis._setRequestError('abort');\n\t\t}\n\n\t\tif (this._readyState === this.DONE) {\n\t\t\tthis._readyState = this.UNSENT;\n\t\t}\n\t}\n\n\tpublic send(data?: any) {\n\t\tthis._errorFlag = false;\n\t\tthis._response = null;\n\t\tthis._responseTextReader = null;\n\t\tthis._headers = null;\n\t\tthis._status = null;\n\n\t\tif (this._readyState !== this.OPENED || this._sendFlag) {\n\t\t\tthrow new Error(\"Failed to execute 'send' on 'XMLHttpRequest': \" + \"The object's state must be OPENED.\");\n\t\t}\n\n\t\tif (isString(data) && this._options.method !== 'GET') {\n\t\t\t//The Android Java HTTP lib throws an exception if we provide a\n\t\t\t//a request body for GET requests, so we avoid doing that.\n\t\t\t//Browser implementations silently ignore it as well.\n\t\t\tthis._options.content = data;\n\t\t} else if (data instanceof FormData) {\n\t\t\tthis._options.content = (<FormData>data).toString();\n\t\t} else if (data instanceof Blob) {\n\t\t\tthis.setRequestHeader('Content-Type', data.type);\n\t\t\tthis._options.content = Blob.InternalAccessor.getBuffer(data);\n\t\t} else if (data instanceof ArrayBuffer) {\n\t\t\tthis._options.content = data;\n\t\t}\n\n\t\tthis._sendFlag = true;\n","sourceCodeStart":215,"sourceCodeEnd":251,"githubUrl":"https://github.com/NativeScript/NativeScript/blob/6800aefa6511d23ddad8beb38ccc8541c5e91628/packages/core/xhr/index.ts#L215-L251","documentation":"XMLHttpRequest.send() in @nativescript/core requires the request to be in the OPENED state (after a successful open() call) and not already sent. The implementation checks this.readyState !== OPENED || this._sendFlag and throws to mirror the browser spec, where send() on an uns opened or already-sent request is an InvalidStateError. It enforces the one-request-per-XHR lifecycle.","triggerScenarios":"Calling xhr.send() before calling xhr.open(); calling send() twice on the same XMLHttpRequest instance (the second call hits _sendFlag === true); calling send() after abort() or after the request reached DONE without resetting via open().","commonSituations":"Event-handler race where an async callback retries send() on the same object; copy-pasted code that forgets open(); wrapping send in retry logic without creating a new XHR instance; calling send from onreadystatechange handlers after completion.","solutions":["Call xhr.open(method, url) before calling send().","Create a fresh XMLHttpRequest instance for each request instead of reusing one.","Guard with a state check: only send when xhr.readyState === xhr.OPENED and not already sent.","If retrying, wrap the open+send sequence in a function that builds a new XHR each time."],"exampleFix":"// before\nconst xhr = new XMLHttpRequest();\nxhr.send(null); // throws\n\n// after\nconst xhr = new XMLHttpRequest();\nxhr.open('GET', url);\nxhr.send(null);","handlingStrategy":"validation","validationCode":"if (xhr.readyState === xhr.OPENED && !xhr._sendFlag) { xhr.send(data); }","typeGuard":"function canSend(xhr) { return xhr.readyState === xhr.OPENED; }","tryCatchPattern":"try { xhr.send(data); } catch (e) { if (String(e.message).includes(\"state must be OPENED\")) { /* create new XHR, open, send */ } else { throw e; } }","preventionTips":["Always pair open() immediately before send().","Never reuse an XHR object for a second request.","Centralize XHR creation in one helper so open+send ordering is guaranteed.","Don't call send() from response handlers."],"tags":["xhr","state-machine","invalid-state"],"backgroundTag":"xhr-invalid-state","analyzedSha":"6800aefa6511d23ddad8beb38ccc8541c5e91628","analyzedAt":"2026-08-30T20:04:56.809Z","schemaVersion":2},"datasetVersion":"2026-08-30T23:17:21.991Z"}