Last active
March 31, 2017 04:53
-
-
Save akirattii/9084759 to your computer and use it in GitHub Desktop.
Simplified function of XMLHttpRequest process on Chrome Apps.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="SimplifiedXMLHttpRequest.js"></script> | |
<script> | |
document.getElementById('btn').onclick = function () { | |
request({ | |
url: "http://www.google.com/", | |
}, myhandler); | |
}; | |
//document.getElementById('btn').onclick = function () { | |
// var requestHeaders = new Array(0); | |
// requestHeaders.push({ key: "Accept", value: "*/*" }); | |
// doRequest({ | |
// url: "http://hoge/foo.txt", | |
// requestHeaders: requestHeaders, | |
// responseType: "text", | |
// mimeType: "text/plain; charset=shift_jis", | |
// }, myhandler); | |
// } | |
function myhandler(err, xhr) { | |
response = xhr.responseXML || xhr.responseText; | |
console.log(response); | |
// something to do | |
}; | |
</script> | |
<button id="btn">Exec</button> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
... | |
# Chrome Apps aren't so limited by the same origin policy, | |
# can talk to remote servers outside of its origin, | |
# as long as it first requests cross-origin permissions. | |
"permissions": [ | |
"<all_urls>" | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* pure js simplified XMLHttpRequest function | |
* | |
* @param {Object} p | |
* { | |
* url: | |
* required. | |
* method: | |
* ('GET' | 'POST') | |
* default is "GET" | |
* formData: eg: `hoge=piyo&moge=fuga` | |
* responseType: | |
* ('document' | 'text') | |
* default is "document" | |
* mimeType: | |
* default is null. | |
* e.g. "text/plain; charset=shift_jis" | |
* requestHeaders: | |
* array of requestHeader. requestHeader is json likes { key:xxx, value:yyy }. | |
* default is null. | |
* async: | |
* default is true | |
* } | |
* @param {Function} - error-first style callback | |
* @link https://gist.github.com/akirattii/9084759 | |
*/ | |
var request = function({ | |
url, | |
method = "GET", | |
mimeType, | |
responseType = "document", | |
async = true, | |
requestHeaders | |
}, cb) { | |
// logger.debug("Requesting..."); | |
if (!url) throw Error(`[request] Invalid parameter: url required`); | |
let xhr = new XMLHttpRequest(); | |
if (mimeType) { | |
xhr.overrideMimeType(mimeType); | |
} | |
xhr.responseType = responseType; | |
xhr.open(method, url, async); | |
if (method && method.toUpperCase() == "POST") { | |
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); | |
} | |
if (requestHeaders && requestHeaders.length >= 1) { | |
for (let len = requestHeaders.length, i = 0; i < len; i++) { | |
xhr.setRequestHeader(requestHeaders[i].key, requestHeaders[i].value); | |
} | |
} | |
xhr.onreadystatechange = function(e) { | |
if (xhr.readyState == 4) { | |
if (xhr.status == 200) | |
cb && cb(null, xhr); | |
else | |
cb && cb(xhr.status, xhr); | |
} | |
}; | |
// xhr.onerror = function(err){ | |
// console.log("onerror",err); | |
// }; | |
xhr.send(p.formData); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment