Skip to content

Instantly share code, notes, and snippets.

@g-liu
Last active April 26, 2016 02:56
Show Gist options
  • Save g-liu/e4c6bb22bdb7b19eb234ff7f82140002 to your computer and use it in GitHub Desktop.
Save g-liu/e4c6bb22bdb7b19eb234ff7f82140002 to your computer and use it in GitHub Desktop.
Code snippet for performing an Ajax request
<snippet>
<content><![CDATA[
var ${1:ajax} = new XMLHttpRequest();
${1:ajax}.onload = ${2:functionName};
${1:ajax}.onerror = ${3:errorFunctionName};
${1:ajax}.open("${4:GET}", "${5:url}", ${6:true});
${1:ajax}.send(${7});
function ${2:functionName}() {
if (this.status == 200) { // request succeeded
${8:// do something with this.responseText};
} else {
${9:// handle more HTTP response codes here};
}
}
function ${3:errorFunctionName}(${10:e}) {
console.error(${10:e});
// do something with this.status, this.statusText
}
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>ajax</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<scope>source.js</scope>
</snippet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment