0

I have developed a site on my local machine and got it working fine, but after uploading it to my remote host, the ajax/json calls fail to run.

I have checked for any html being written before the redirects are run and the only things I can find are where I am echoing json_encode($return).

Is this the problem? And if so does anyone know how I can fix this.? An alternative to using echo maybe??

thanks

Code below with labels as to where I think the problem might be...

<script type="text/javascript">

$(document).ready( function() {
    $("#addStory input[type=submit]").click(function(e) {
        e.preventDefault();
        $.post('editor.php', $("#addStory").serialize(), function(result) {
            alert(result.adminList);
        }, "json");
    });
});

</script>



<form name="addStory" action="" method="post" id="addStory">
    <input type="text" id="test_text" name="test_text" />
    <input type="submit" value="Submit" />
</form>

And here's editor.php...

<?php

header('application/json');

$return = array();
$return['adminList'] = "Hello World!";
echo json_encode($return);

?>

The "Hello World" alert does not happen...

10
  • 1
    Where is the header('location: ...) call? I don't see it in your code. Commented Jul 13, 2012 at 10:34
  • That's a good point... I've picked a bad example! However, this shows the ajax not running properly because the I am using a javascript redirect in this example. If the user enters correct login details $return['adminlist'] populates the javascript redirect value. Hope this makes a bit of sense, I'm not sure how best to explain. Commented Jul 13, 2012 at 10:40
  • Well what errors are you getting? Is there a javascript error or is your php not returning? You need more information for us to come up with an answer. However my answer below does point out one error in your php. Commented Jul 13, 2012 at 10:51
  • I'm not getting any javascript errors as far as I can tell. The php is running fine as all the sessions are being set correctly. I'll see if I can work out how to throw back any errors that I might not be seeing currently. Commented Jul 13, 2012 at 11:00
  • If you want to display the errors then just alert the return in your javascruipt ajax success statement. Commented Jul 13, 2012 at 11:07

1 Answer 1

2

One problem you do have is the second header('application/json') is placed after the output of the first SQL query. This is only the case if $row['column_name'] == 0 is true though.

If it is always going to return json then just set this right at the top of the php code to avoid confusion later on.

<?php
    header('application/json');

    //all your sql etc here

?>

I also noticed during our discussion that your header is actually incorrect. It should be

header('Content-type: application/json');

Removing it would also work however this is probably a bad idea since this serves the purpose of telling the recipient what type of data to expect.

Another point is that your current ajax should really be taking advantage of the getJSON() function in jQuery which will automatically give you back a json object.

$.getJSON('example.php', function(data) {
    //use data as a json object
}); 
Sign up to request clarification or add additional context in comments.

2 Comments

If moved this as a one off to the top of the page and the PHP stopped running. What's really odd about this is that it works perfectly on my local machine.
@Tom ive edited my answer, hopefully this covers everthing we talked about, note the header change too, not sure if you got my last message in the chat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.