Mackski Posted February 19, 2017 Posted February 19, 2017 $config->ajax is being set locally, however the exact same call for my client it is not. The request for my client is rather long, 25 seconds. However, I'm stumped as to why, this should not affect this code: $config->ajax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); If I remove the if($config->ajax) from my ajax function, everything works as expected. Headers sent look correct: Accept:application/json, text/javascript, */*; q=0.01 Accept-Encoding:gzip, deflate, sdch Accept-Language:en-US,en;q=0.8 Connection:keep-alive Cookie:wire=vg8bth2bcv2c8gq58nbbfean61; wire_challenge=nVbLGO0aHzCu%2FMO9ekc4t.N6ol.xf8s5 Host:** Referer:http://*** User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36 X-Requested-With:XMLHttpRequest
LostKobrakai Posted February 19, 2017 Posted February 19, 2017 Do I understand this correctly, that the client does test the site on a different server than you local one? The question is not only if headers are correctly sent, but also if those headers make it to the server. Some hosters have crazy setups with nginx or even varnish in front of the actual webserver, so things could easily get lost on the way.
Mackski Posted February 19, 2017 Author Posted February 19, 2017 The client is actually accessing my local server. I've tested with some other clients who don't see this issue. Strangly it's only happening in one page. My Ajax call is sent to a different URL within the project. The same call is also being used elsewhere without issue.
Robin S Posted February 19, 2017 Posted February 19, 2017 21 hours ago, Mackski said: $config->ajax = (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'); I'm probably missing something, but not sure why you are overriding $config->ajax here. This variable is set for you by PW when the current request is AJAX - you shouldn't have to set this yourself.
Mackski Posted February 19, 2017 Author Posted February 19, 2017 25 minutes ago, Robin S said: I'm probably missing something, but not sure why you are overriding $config->ajax here. This variable is set for you by PW when the current request is AJAX - you shouldn't have to set this yourself. No, I was just showing the line of code which appears to not work.
LostKobrakai Posted February 20, 2017 Posted February 20, 2017 Did you try to log the contents of $_SERVER['HTTP_X_REQUESTED_WITH']? Maybe this header is stripped out anywhere on the way to your local server.
Mackski Posted February 20, 2017 Author Posted February 20, 2017 Yes for this one call the headers are striped but I have no idea why or where this is happening.
Wanze Posted February 20, 2017 Posted February 20, 2017 Can you find out on which browser this is happening? I remember having this problem once when a JQuery plugin was sending the ajax requests - of course only with an ancient version of Internet Explorer
hellomoto Posted Saturday at 11:45 PM Posted Saturday at 11:45 PM (edited) https://github.com/processwire/processwire/blob/44fcf13ea2d7f14a04eed54c29afcc79eb46ec45/wire/core/Config.php#L24 Searching the file for "ajax" only shows the 2 instances in the 1st comment (same line); $config->ajax is doing nothing when I try using it? I make a call to a page and it loads in the element, but $config->ajax is false in the ajax-loaded page. It works with jQuery; I was trying without it function loadlist(segment='') { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("main").innerHTML = this.responseText; } }; xhttp.open("GET", "<?=$page->url?>"+segment, true); xhttp.send(); } Edited yesterday at 12:08 AM by hellomoto it works with jquery
adrian Posted yesterday at 01:10 AM Posted yesterday at 01:10 AM Add this line before your xhttp.open line and it should work: xhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
hellomoto Posted yesterday at 02:43 AM Posted yesterday at 02:43 AM 1 hour ago, adrian said: Add this line before your xhttp.open line and it should work: xhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest"); It didn't for me, thanks though
adrian Posted yesterday at 02:58 AM Posted yesterday at 02:58 AM 13 minutes ago, hellomoto said: It didn't for me, thanks though Sorry, it needs to be after the open line. I just tested here and it works as expected when placed after.
adrian Posted yesterday at 03:00 AM Posted yesterday at 03:00 AM <?php namespace ProcessWire; if($config->ajax) { bd('AJAX request detected'); echo "Replaced page content"; exit(); } ?> <script> function loadlist(segment='') { var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { document.getElementById("main").innerHTML = this.responseText; } }; xhttp.open("GET", "<?=$page->url?>"+segment, true); xhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest"); xhttp.send(); } </script> <div id="content"> <div id="main">Basic page content</div> <p><button onclick="loadlist('')">Load List</button></p> </div> Note the replacement of the text in "main" and that Tracy detects the AJAX request. 2
hellomoto Posted 7 hours ago Posted 7 hours ago (edited) Putting the line after xhttp.open does seem to make more sense. Sorry, I failed to reasonably troubleshoot that there. Thank you. It works but not as reliably as jQuery? Edited 7 hours ago by hellomoto
adrian Posted 7 hours ago Posted 7 hours ago What do you mean by "not as reliably"? Does it fail sometimes? I use this in so many places and I don't think I've ever had an issue so perhaps there is something else at play here.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now