Forum
Ajax returning empty from perch_content_custom
Hi guys, I've been banging away trying to get ajax content to work for hours, everything seems ok, except that the data returned from my PHP code is empty when it gets back to the jQuery code. When I run the PHP code directly it outputs the corrects HTML just as I'd expect, but when its returned through Ajax it's empty, there's nothing, it's as if it's been stripped or something - possibly a security issue with Perch?
EDIT: Just to be a little more clear, it does return an object to the jQuery code, the object is just empty. When I set 'return-html' as true, it return the 'html' array, but it's also completely empty.
I've included some code below.
Perch PHP Stuff
<?php if (!defined('PERCH_RUNWAY')) include($_SERVER['DOCUMENT_ROOT'].'/perch/runtime.php');
echo json_encode(
perch_content_custom('Fish Profile', array(
'template' => '/fish-collection/ajax-fish.html',
'filter' => 'fish-title',
'value' => perch_get('title'),
'match' => 'eq',
'count' => 1,
'skip-template' => true
))
);
This is just a simple request to get some content which is being filtered using a variable which is passed through in the ajax request.
jQuery Ajax Request
$.post('/ajax-fish.php', {'title':fishTitle}, function(data)
{
var json = $.parseJSON(data);
console.log(json);
$('div.fish-item-content[data-item='+itemNumber+']').append(json);
});
I would VERY much appreciate any help with this!
Thanks! Andrew
I wonder if it's a headers issue. Try explicitly setting it as JSON:
Thanks Drew, Yeah I already tried setting the headers explicitly as json, it didn't make a difference unfortunately.
It's as though the output from the perch_custom_content tag is either not being echoed at all or the echoed data is being removed when it's returned to the Ajax request. As I said it does return data, it's just always empty - if I turn on return-html it returns the html array, it's just empty also.
I was wondering if it might be a same origin policy issue or something, even though it's all on the same domain. Actually, that's probably not the problem as I can return some dummy data from php fine, it's just returning the perch data that won't work
I would LOVE to understand what's actually going on here!
What do you get if you view the JSON page directly?
Do you otherwise get any errors in the browser console?
Thanks Drew, When I navigate to the main page directly I get the expected output without errors - under some circumstances (I've tried a whole bunch of perch_custom_content setups to try to get it to work) it also has the word 'null' at the very end of the output. It seems as though I just get that 'null' returned, or in the situations where there is no null returned I just get the empty object.
I suspect the perch_custom_content is possibly not being echoed, but is just being output directly and is therefore not being returned to the Ajax request. It seems like what is being echoed is the 'null' or empty object, which is being returned to Ajax.
Could it be that perch_custom_content is outputting its results directly and when win I try to echo it, for some reason that data is gone so I get either an empty object or null returned to Ajax?
Just to confirm, returning data from the PHP script works perfectly unless it's content generated from perch - if it comes from perch the data is all lost and just an empty object is returned otherwise all the data is returned exactly as expected
OK - solved. Turns out I'm a moron, an extremely frustrated moron. I was using a post ajax request (which doesn't work for some reason) instead of a get request, using $.get instead and it works.
EDIT: Out of curiosity, is it ever possible to use a POST request to send data to the server using Perch? It seems like Perch treats all posted data as a GET request - when I tried to use the POST request, the data was still only available in PHP as a $_GET variable
It makes no difference to Perch. Do you have any server side redirects in place that might affect the URL you're using? Get parameters would make it through a redirect whereas Post parameters could be lost.
Yeah, that's a possiblity - I'm using a vagrant box for development, it may be effecting it in some way, although I think there is some perch involvement too because when I do a post request without using perch it works fine. Ajax post requests on this dev server have always worked fine in the past with other systems too, just not with Perch.