You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
$.ajax('myservice/username',{data: {id: 'some-unique-id'}}).then(functionsuccess(name){alert('User\'s name is '+name);},functionfail(data,status){alert('Request failed. Returned status of '+status);});
Native XMLHttpRequest Object
varxhr=newXMLHttpRequest();xhr.open('GET','myservice/username?id=some-unique-id');xhr.onload=function(){if(xhr.status===200){alert('User\'s name is '+xhr.responseText);}else{alert('Request failed. Returned status of '+xhr.status);}};xhr.send();
jQuery
varnewName='John Smith';$.ajax('myservice/username?'+$.param({id: 'some-unique-id'}),{method: 'POST',data: {name: newName}}).then(functionsuccess(name){if(name!==newName){alert('Something went wrong. Name is now '+name);}},functionfail(data,status){alert('Request failed. Returned status of '+status);});
Native XMLHttpRequest Object
varnewName='John Smith',xhr=newXMLHttpRequest();xhr.open('POST','myservice/username?id=some-unique-id');xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');xhr.onload=function(){if(xhr.status===200&&xhr.responseText!==newName){alert('Something went wrong. Name is now '+xhr.responseText);}elseif(xhr.status!==200){alert('Request failed. Returned status of '+xhr.status);}};xhr.send(encodeURI('name='+newName));