i have onRequestEnd Method and have some javascrip...
# cfml-general
g
i have onRequestEnd Method and have some javascript code in that, that is basically breaking all ajaxc alls of my page, how can i fix that
j
You can do a check to not include if the request is an
XMLHttpRequest
by looking for this value in the
["X-Requested-With"]
header. If you are using fetch in the JS side, then this useful header is not included but you can add headers to your fetch calls. I tend to add it and just add the word fetch. Here is a function I have that I use in many places. Place it in your Application.cfc and then you can use it wherever you want in it or within your views. But I too recommend that you do not try to inject stuff @
onRequestEnd()
especially if you are not inspecting the output and then re-injecting before your body close tag to retain proper structure.
Copy code
public boolean function isAjax(){
		var req = getHttpRequestData().headers;
		return structKeyExists(req,"X-Requested-With") && listFindNoCase("fetch,XMLHttpRequest",req["X-Requested-With"]) ? true : false;
}
a
Yeah,
X-Requested-With
is not a standard HTTP header but quite a few Javascript libraries do set it for you (like jQuery) so may already be there.
g
But I am not using fetch I am using ruthe Ajax or post or get in the Ajax call
j
What are you using to make you AJAX request? Like @aliaspooryorik stated, if you are using jQuery, that header is included and if not, it should be fairly easy to add a header to your request if just using vanilla JavaScript.
g
is a legacy app with mix and match of jquery adn javascript
j
Then that means you are most likely using jQuery for Ajax requests, so go ahead and try using that function to omit your JS that you add at onRequestEnd.
g
ok