webapp.js.server.js Maven / Gradle / Ivy
The newest version!
var content = "#contentOfSite";
var changeURL = true;
var srv = "/srv";
//Get template by sending ajax request 'GET or 'POST' according to parameter type
function trans(obj) {
if (typeof obj === 'string') {
transGET(obj);
} else if (typeof obj === 'object') {
transPOST(obj);
} else {
return false;
}
}
//get generated html from server by sending ajax 'GET' request
function transGET(url) {
url = url.replace('#', '%23');//encode '#'
$.ajax({
type: 'GET',
url: url,
crossDomain: true,
success: function (response) {
success(response, url);
},
error: function (response, status, error) {
error(response, error);
}
});
}
//get generated html from server by sending ajax 'POST' request
function transPOST(form) {
var fd = new FormData(form);
if (form.id === '') {
alert('Please give a unique name to the form.');
return false;
}
var url = $('#' + form.id).attr('action');//get relative path
$.ajax({
type: 'POST',
url: url,
data: fd,
enctype: "multipart/form-data",
processData: false,
contentType: false,
crossDomain: true,
success: function (response) {
success(response, url);
},
error: function (response, status, err) {
error(response, err, url);
}
});
}
//return 500 error
function error(response, err, url) {
var text = '' + err + ' (error code: ' + response.status + ')
' + response.responseText + '';
$(content).html(text);
updateUrl(url);
}
//when ajax returns '200 ok', display the response text on the page
function success(response, url) {
var text = '' + response + '';
$(content).html(text);
updateUrl(url); //2 change the url displayed in broswer url bar
correct();
}
//jquery.html() method auto close the htmls tags, even for the text value of © 2015 - 2025 Weber Informatics LLC | Privacy Policy