How to create Document objects with JavaScript

There are two methods defined in specifications, createDocument from DOM Core Level 2 and createHTMLDocument from HTML5. The former creates an XML document (including XHTML), the latter creates a HTML document. Both reside, as functions, on the DOMImplementation interface. var impl = document.implementation, xmlDoc = impl.createDocument(namespaceURI, qualifiedNameStr, documentType), htmlDoc = impl.createHTMLDocument(title); In reality, these methods … Read more

Prompt file download with XMLHttpRequest

If you set the XMLHttpRequest.responseType property to ‘blob’ before sending the request, then when you get the response back, it will be represented as a blob. You can then save the blob to a temporary file and navigate to it. var postData = new FormData(); postData.append(‘cells’, JSON.stringify(output)); var xhr = new XMLHttpRequest(); xhr.open(‘POST’, ‘/export/’, true); … Read more

Return $.get data in a function using jQuery

You have a few different mistakes. First, $.get doesn’t return the return value of the callback function. It returns the XHR object. Second, the get function isn’t synchronous, it’s asynchronous so showGetResult will likely return before get completes. Third, you can’t return something from inside the callback to the outer scope. You can, however, bind … Read more

Authorization in RESTful HTTP API, 401 WWW-Authenticate

To answer your questions: How to deal with unauthorized requests? The way you described it is pretty much the recommended way for a RESTful service. As far as I can see there is absolutely nothing wrong with that. What WWW-Authenticate header should 401 responses supply? In general the WWW-Authenticate header tells the client what kind … Read more

In JavaScript how do I/should I use async/await with XMLHttpRequest?

I usually do async/await like this: async function doAjaxThings() { // await code here let result = await makeRequest(“GET”, url); // code below here will only execute when await makeRequest() finished loading console.log(result); } document.addEventListener(“DOMContentLoaded”, function () { doAjaxThings(); // create and manipulate your DOM here. doAjaxThings() will run asynchronously and not block your DOM … Read more

How do I send an HTTP request from a Chrome extension?

First, you’ll need to edit your manifest.json and add the permission for www.example.com: For the new Manifest V3, use the host_permissions field: { “manifest_version”: 3, … “host_permissions”: [ “https://www.example.com/*” ], … } If you are still using the old Manifest V2, use the permissions field: { “manifest_version”: 2, … “permissions”: [ “https://www.example.com/*” ], … } … Read more

Timeout XMLHttpRequest

using the timeout properties of XMLHttpRequest object for example. var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function () { if (xhr.readyState == 4) { alert(“ready state = 4”); } }; xhr.open(“POST”, “http://www.service.org/myService.svc/Method”, true); xhr.setRequestHeader(“Content-type”, “application/json; charset=utf-8”); xhr.timeout = 4000; // Set timeout to 4 seconds (4000 milliseconds) xhr.ontimeout = function () { alert(“Timed out!!!”); } … Read more

Detect browser support for cross-domain XMLHttpRequests?

For future reference, full CORS feature detection should look something like this: //Detect browser support for CORS if (‘withCredentials’ in new XMLHttpRequest()) { /* supports cross-domain requests */ document.write(“CORS supported (XHR)”); } else if(typeof XDomainRequest !== “undefined”){ //Use IE-specific “CORS” code with XDR document.write(“CORS supported (XDR)”); }else{ //Time to retreat with a fallback or polyfill … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)