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…
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'] == 0is 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.
I also noticed during our discussion that your header is actually incorrect. It should be
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.