Jump to content

Recommended Posts

Posted

$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

 

Posted

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.

Posted

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. 

Posted
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.

Posted
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.

Posted

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 ;)

  • 8 years later...
Posted (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 by hellomoto
it works with jquery
Posted
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

Posted
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.

 

Posted
<?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.

Screen Recording 2025-12-07 at 12.59.27 PM.gif

  • Like 2
Posted (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 by hellomoto
Posted

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.

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...