make ajax call from javascript code example
Example 1: ajax call
$.ajax({
url: "Url",
dataType: "json",
type: "Post",
async: true,
data: {"Key":value,"Key2":value2},
success: function (data) {
},
error: function (xhr, exception, thrownError) {
var msg = "";
if (xhr.status === 0) {
msg = "Not connect.\n Verify Network.";
} else if (xhr.status == 404) {
msg = "Requested page not found. [404]";
} else if (xhr.status == 500) {
msg = "Internal Server Error [500].";
} else if (exception === "parsererror") {
msg = "Requested JSON parse failed.";
} else if (exception === "timeout") {
msg = "Time out error.";
} else if (exception === "abort") {
msg = "Ajax request aborted.";
} else {
msg = "Error:" + xhr.status + " " + xhr.responseText;
}
if (callbackError) {
callbackError(msg);
}
}
});
Example 2: ajax syntax in javascript
var id = empid;
$.ajax({
type: "POST",
url: "../Webservices/EmployeeService.asmx/GetEmployeeOrders",
data: "{empid: " + empid + "}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result){
alert(result.d);
console.log(result);
}
});
Example 3: js functional ajax requests
function checkUserIdExists(userid, callback) {
return $.ajax({
url: 'theurl',
type: 'GET',
cache: false,
data: {
userid: userid
}
})
.done(callback)
.fail(function(jqXHR, textStatus, errorThrown) {
});
}
checkUserIdExists(2, function(data) {
console.log(data);
});