Using client script, we can make call to service. the service itself will check for the Authentication and authorization. below is the Script to make a GET call.
$.ajax({
type: 'Get',
beforeSend: function (xhr, settings) {
xhr.setRequestHeader('X-CSRF-Token', "Fetch");
},
cache: false,
url: 'URL here'
dataType: 'xml',
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function (response, status, xhr) {
globalToken = xhr.getResponseHeader('X-CSRF-Token');
console.log(globalToken);
},
error: function (xhr, status, error) {
console.log(xhr);
console.log(status);
console.log(error);
if (xhr.status == 404) {
errorMessage = error;
} else {
errorMessage = xhr.responseJSON.error.message.value;
}
},
complete: function (xhr, status) {
if (xhr.status === 200) {
//next action here
}
}
});
POST
$.ajax({
type: 'Get',
beforeSend: function (xhr, settings) {
xhr.setRequestHeader('X-CSRF-Token', "Fetch");
},
cache: false,
url: 'GET Url here'
dataType: 'xml',
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function (response, status, xhr) {
globalToken = xhr.getResponseHeader('X-CSRF-Token');
console.log(globalToken);
},
error: function (xhr, status, error) {
console.log(xhr);
console.log(status);
console.log(error);
if (xhr.status == 404) {
errorMessage = error;
} else {
errorMessage = xhr.responseJSON.error.message.value;
}
},
complete: function (xhr, status) {
if (xhr.status === 200) {
if (globalToken != '') {
// CALL POST to SAP
$.ajax({
async: false,
url: 'POST Url here'
beforeSend: function (xhr) {
xhr.setRequestHeader('X-CSRF-Token', globalToken);
},
type: 'Post',
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.Stringify(postJsonObjectString),
crossDomain: true,
success: function (response, status, xhr) {
output= response.OutputNumber;
},
error: function (xhr, status, error) {
console.log(xhr);
console.log(status);
console.log(error);
console.log(xhr.responseJSON.error.code);
},
complete: function (xhr, status) {
if (xhr.status === 201) {
}
else {
}
}
});
} else {
alert('Failed to receive Token.');
}
}
else {
alert('Failed to receive Token.');
}
}
});
$.ajax({
type: 'Get',
beforeSend: function (xhr, settings) {
xhr.setRequestHeader('X-CSRF-Token', "Fetch");
},
cache: false,
url: 'URL here'
dataType: 'xml',
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function (response, status, xhr) {
globalToken = xhr.getResponseHeader('X-CSRF-Token');
console.log(globalToken);
},
error: function (xhr, status, error) {
console.log(xhr);
console.log(status);
console.log(error);
if (xhr.status == 404) {
errorMessage = error;
} else {
errorMessage = xhr.responseJSON.error.message.value;
}
},
complete: function (xhr, status) {
if (xhr.status === 200) {
//next action here
}
}
});
POST
$.ajax({
type: 'Get',
beforeSend: function (xhr, settings) {
xhr.setRequestHeader('X-CSRF-Token', "Fetch");
},
cache: false,
url: 'GET Url here'
dataType: 'xml',
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function (response, status, xhr) {
globalToken = xhr.getResponseHeader('X-CSRF-Token');
console.log(globalToken);
},
error: function (xhr, status, error) {
console.log(xhr);
console.log(status);
console.log(error);
if (xhr.status == 404) {
errorMessage = error;
} else {
errorMessage = xhr.responseJSON.error.message.value;
}
},
complete: function (xhr, status) {
if (xhr.status === 200) {
if (globalToken != '') {
// CALL POST to SAP
$.ajax({
async: false,
url: 'POST Url here'
beforeSend: function (xhr) {
xhr.setRequestHeader('X-CSRF-Token', globalToken);
},
type: 'Post',
dataType: "json",
contentType: "application/json; charset=utf-8",
data: JSON.Stringify(postJsonObjectString),
crossDomain: true,
success: function (response, status, xhr) {
output= response.OutputNumber;
},
error: function (xhr, status, error) {
console.log(xhr);
console.log(status);
console.log(error);
console.log(xhr.responseJSON.error.code);
},
complete: function (xhr, status) {
if (xhr.status === 201) {
}
else {
}
}
});
} else {
alert('Failed to receive Token.');
}
}
else {
alert('Failed to receive Token.');
}
}
});
hi
ReplyDelete