Skip to content

Instantly share code, notes, and snippets.

@JamesIgoe
Created March 5, 2020 17:54
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 JamesIgoe/1acb284d59df31c5ebb79a76be1c4cae to your computer and use it in GitHub Desktop.
Save JamesIgoe/1acb284d59df31c5ebb79a76be1c4cae to your computer and use it in GitHub Desktop.
Variety of POST examples in JavaScript
function postMessageXhr() {
var xhr = new XMLHttpRequest();
xhr.open("POST", "", true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.send(JSON.stringify({
ApplicationName: "",
ComputerName: "",
OperatingSystem: "",
UserName: "",
LogLevel: "",
Message: ""
}));
}
function postMessageAjax() {
$.ajaxSetup({
crossDomain: true,
xhrFields: {
withCredentials: true
},
});
$.ajax({
url: '',
data: JSON.stringify({
ApplicationName: '',
ComputerName: '',
OperatingSystem: '',
UserName: '',
LogLevel: '',
Message: ''
}),
dataType: "json",
contentType: 'application/json',
type: 'POST',
error: function (xhr, status, error) {
app.showNotification(_appName, error);
}
}).done(function (data) {
app.showNotification(_appName, data);
});
}
function postMessageJquery() {
$.post("", {
ApplicationName: '',
ComputerName: '',
OperatingSystem: '',
UserName: '',
LogLevel: '',
Message: ''
}).done(function (data) {
app.showNotification(_appName, data);
}).fail(function (xhr, status, error) {
app.showNotification(_appName, xhr.statusText);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment