Jump to content

Ajax data being stripped from URL


bbb
 Share

Recommended Posts

I'm in the process of rebuilding a WordPress site over into ProcessWire (Yay!)

I'm using Ajax to call a page (ajaxAgenda) from the homepage which includes a PHP file (partialAgenda) which makes the call.

This is what I got in the included partial. Notice that the URL has a / at the end to prevent redirection.

var page_number = 1;
var get_agenda = function(){
	$.ajax({
		type       : "GET",
		data	   : {pnum: page_number},
		dataType   : "html",
		url        : "/components/ajaxAgenda/",
		beforeSend : function(){
			$('#ajax-loader2').show();
		},
		success    : function(data){
			$('#ajax-loader2').hide();
			$('#agenda').html(data);
		}
	});
}

And in the Ajax called file I have the following

echo $page->url.'<br>';
echo $input->get->pnum.'<br>';
echo $_GET['pnum'].'<br>';

which only outputs "/components/ajaxagenda/". pnum is empty and get is also also empty

It seems like the variables are simply being stripped from my Ajax call. Or am I missing something really obvious?

Link to comment
Share on other sites

I tried the following:

I put the file I call through ajax in the root (so I'm simply calling a php file rather than a page with a template) and am able to read out the get variable that way. But I'd rather not do it this way and would like to maintain calling a page with a get ?variable. Is there some kind of setting I'm missing?

Link to comment
Share on other sites

I'm not sure what's behind the issue you're seeing, but I tested basically the same code and it works for me.

In the source page:

<script>
    $(function() {
        var page_number = 12345;
        $.ajax({
            type: "GET",
            data: {pnum: page_number},
            dataType: "html",
            url: "/two/",
            success: function(data) {
                console.log('data: ' + data);
            }
        });
    });
</script>

And in the template for page "two":

if($config->ajax) {
    $num = $input->get->pnum;
    echo "Num: $num";
    return $this->halt();
}

Logged to console:

data: Num: 12345

 

Link to comment
Share on other sites

Does it still work for you when you include the given source page as separate php file using WireIncludeFile()? My exact situation is as follows:

 page using basic-page.php template which uses WireIncludeFile() to include partialAgenda.php where the ajax call is which calls a page using ajaxAgenda.php template

Link to comment
Share on other sites

Okay, so I got this working by turning on URL segments and retrieving the segment[n] in the called file, still haven't figured out why it doesn't work with GET  variables.

Link to comment
Share on other sites

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
 Share

×
×
  • Create New...