Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

I was developing a site on my computer and was able to get AJAX requests with jQuery working fairly painlessly. On the production server, however, I'm running into problems.

This is how the CodeIgniter controller I have returns a result:

# Return that everything worked.
$return = array('result' => 'success', 'data' => $item);
echo json_encode($return);

And this is some sample jQuery code:

$.post("../../../list/vote/", { 'item_id' : item_id, 'value' : 0, 'action' : 'delete' },
function(data) {
    if(data.result || !data.result) {
        if(votes == null) votes = 0;
        $(button).children('strong').text(--votes);
        $(button).attr('name', '').removeClass('vote_cast');
        $(button).parent('.meta').attr('name', '');
    }
}, "json");

The problem is that PHP is returning a 200 OK status but no response is shown in Firebug. Since nothing is being returned, jQuery is saying data is null.

I'm using MAMP locally (PHP/5.3.5) and PHP/5.3.2-1ubuntu4.9 on the server. It works locally but not on the server. What could be wrong?

EDIT: Some more info. This is happening for every AJAX request on my site in the production server. The request actually goes through (votes are cast, items are added, etc...), it's just that jQuery isn't informed of the result (which explains why data is null).

UPDATE: Scott Harwell figured it out. After I echo'd the json_encode, PHP continued trying to process the rest of the code. It was fixed by adding exit(); after each echo json_encode();.

share|improve this question
1  
What is this condition if(data.result || !data.result) for? – ShankarSangoli Aug 7 '11 at 0:16
    
All the paths are the same? Make sure the right controller is answering on your request. – veritas Aug 7 '11 at 0:16
    
It's for a voting system I have on the site. That's not the problem though because none of the AJAX requests are getting responses on the production server while they all work on the local server. Also, while they don't get responses, the request did indeed work since items are added, votes are cast, etc... – Daniel O'Connor Aug 7 '11 at 0:23
    
This definitely sounds like the ajax call is not getting to the script. Per my answer below, I think you need to verify the URL or file path for the request you are making since the relative path you have writing in your script is probably different on the server. – Scott Harwell Aug 7 '11 at 0:25
    
Just tried that but it still didn't work. It's definitely getting to the script since the PHP actually processes. It just isn't returning anything. In fact, I think it's not even a problem with jQuery since the response tab in Firebug doesn't show anything. When you reload the page, however, you see the new item, vote cast, etc... – Daniel O'Connor Aug 7 '11 at 0:33

1 Answer 1

up vote 1 down vote accepted

Looks like you are accessing the vote page with relative paths. Have you tried explicitly writing the path or give a proper url instead of ../../../list/vote?

share|improve this answer
    
Just tried that but it still didn't work. It's definitely getting to the script since the PHP actually processes. It just isn't returning anything. In fact, I think it's not even a problem with jQuery since the response tab in Firebug doesn't show anything. When you reload the page, however, you see the new item, vote cast, etc... – Daniel O'Connor Aug 7 '11 at 0:38
    
Does anything occur after you echo your encoded string in your php script? The server may be outputting other data that's throwing it off. Try putting exit; after your json_encode just to see if that works. – Scott Harwell Aug 7 '11 at 0:42
    
That did the trick! Thank you so much! Why is it that my code always tends to break when I upload it to the production server? Are the default settings more lenient on MAMP? – Daniel O'Connor Aug 7 '11 at 0:46
    
More than likely, your server is sending back other data (that is breaking the json string) or headers that are conflicting with jQuery. Using the exit method isn't really a solution, just a test to see where the problem is. I would review your php code to make sure there is no other output as well as check the response headers you get from the server. More than likely, your problem is somewhere in there. – Scott Harwell Aug 7 '11 at 0:49
    
This is what the entire function looks like: pastebin.com/aG5ghqj2 How could I fix it then so that PHP stops after the echo (other than the exit method you described)? – Daniel O'Connor Aug 7 '11 at 0:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.