Skip to content

Instantly share code, notes, and snippets.

@gregglind
Last active August 29, 2015 14:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gregglind/8749e3b0658e28d18554 to your computer and use it in GitHub Desktop.
Save gregglind/8749e3b0658e28d18554 to your computer and use it in GitHub Desktop.
/** CORS test script
tools > web developer > web console.
Paste and run.
Results:
- if CORS off, will `console.log` about it.
- else will log 201, or 400.
*/
var Cors = function(url, dataObject) {
var req = new XMLHttpRequest();
req.open('post', url);
req.setRequestHeader("Content-Type", 'application/json'); // will trigger pre-flight
req.onload = function() {
console.log(req.status, req.statusText, req.getAllResponseHeaders());
if (req.status >= 200 && req.status < 300) {
console.log(req.response);
} else {
console.error(Error(req.statusText));
}
};
req.onerror = function(e) {
console.log(e);
console.error(Error('Network Error'));
};
req.send(JSON.stringify(dataObject || {}));
};
var Data = function () {
return {
"person_id": "gregg",
"survey_id": "lunch",
"flow_id": "12345",
"experiment_version": "1",
"response_version": 1,
"question_id": "howwaslunch",
"question_text": "how was lunch?",
"variation_id": "1",
"updated_ts": Date.now(),
"is_test": true
}
}
//Cors("http://127.0.0.1:5000/"+Date.now());
Cors("https://input.mozilla.org/api/v2/hb/", Data())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment