gsr
09/19/2024, 12:31 PMaliaspooryorik
gsr
09/19/2024, 1:09 PMdocument.addEventListener('DOMContentLoaded', function() {
var statsData = {
browser: navigator.userAgent,
operatingSystem: navigator.platform,
device: /Mobile|iP(hone|od|ad)|Android|BlackBerry|IEMobile/.test(navigator.userAgent) ? 'Mobile' : 'Desktop',
screenResolution: screen.width + 'x' + screen.height,
screenSize: window.innerWidth + 'x' + window.innerHeight,
pageUrl: window.location.pathname
};
// Send data to server
fetch('trackStats.cfm', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(statsData)
});
});
and everytime this page was called, it was going into 403 security issue, so i have to comment the code to make it work, there are ajax forms too which i want to make it work but something was not right so not sure what i should doaliaspooryorik
X-CSRF-Token
header though, so you could just set that in your AJAX call.aliaspooryorik
aliaspooryorik
boolean function isAjax(){
var headers = GetHttpRequestData().headers;
if ( structKeyExists( headers, "X-Requested-With" ) ) {
return headers[ "X-Requested-With" ] == "XMLHttpRequest";
}
return false;
}
aliaspooryorik
gsr
09/19/2024, 3:22 PMfunction safeAjax(options) {
// Default options
const defaults = {
url: '',
method: 'GET',
data: null,
headers: {},
csrfToken: null,
useFormData: false
};
// Merge provided options with defaults
const settings = { ...defaults, ...options };
// Add CSRF token to headers if provided
if (settings.csrfToken) {
settings.headers['X-CSRF-TOKEN'] = settings.csrfToken;
}
// Prepare the request data
let requestData = settings.data;
if (settings.useFormData && !(settings.data instanceof FormData)) {
requestData = new FormData();
for (const key in settings.data) {
requestData.append(key, settings.data[key]);
}
}
// Function to handle the response
const handleResponse = (response) => {
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return response.json();
};
// Perform the request using fetch
if (window.fetch) {
return fetch(settings.url, {
method: settings.method,
headers: settings.headers,
body: requestData
}).then(handleResponse);
}
// Fallback to XMLHttpRequest if fetch is not available
else {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();
xhr.open(settings.method, settings.url, true);
// Set headers
for (const header in settings.headers) {
xhr.setRequestHeader(header, settings.headers[header]);
}
xhr.onload = function() {
if (this.status >= 200 && this.status < 300) {
resolve(JSON.parse(xhr.response));
} else {
reject(new Error(`HTTP error! status: ${xhr.status}`));
}
};
xhr.onerror = function() {
reject(new Error('Network error'));
};
xhr.send(requestData);
});
}
}
but this piece of code is still a problem, its unable to inject the token in the forms
<cffunction name="onRequestEnd" returntype="void">
<cfargument name="targetPage" type="string" required="true">
<cfset var response = getPageContext().getResponse()>
<cfset var contentType = response.getContentType()>
<cfdump var="#response#">
<<cfif isNull(contentType) or contentType contains "text/html">
<cfoutput>#injectCsrfToken(contentType)#</cfoutput>
</cfif>
</cffunction>aliaspooryorik
aliaspooryorik
gsr
09/19/2024, 3:47 PMaliaspooryorik
X-CSRF-TOKEN
header? If NO then your JS needs work, if yes your CF needs work.gsr
09/19/2024, 3:48 PMaliaspooryorik
aliaspooryorik
gsr
09/19/2024, 3:51 PMgsr
09/19/2024, 3:51 PMaliaspooryorik
gsr
09/19/2024, 3:52 PMgsr
09/19/2024, 3:52 PMaliaspooryorik
gsr
09/19/2024, 3:54 PMaliaspooryorik
aliaspooryorik
window.fetch
isn't supported then the browser won't like const
either.aliaspooryorik
windows.fetch
(modern) and then add support for older browsers if you need togsr
09/19/2024, 4:01 PM