Reputation: 1
So my ajax call works fine, i get a green light next to it in web inspector and I see the correct parameters being sent back and JSON objects being returned. However, it automatically goes to ERROR. I added some error handling and have ran out of ideas..
Does anyone see anything??
Javascript
function getMoreOlder(id) {
$.ajax({
url: "getmore.php",
type : "POST",
dataType : "json",
data: {
postid : id,
type: "later",
},
success: function(response){
alert("Success");
for (var i=0; i<json.length; i++) {
// alert("Loop");
}
},
error: function(jqXHR,error, errorThrown){
if(jqXHR.status&&jqXHR.status==400){
alert(jqXHR.responseText);
}else{
alert("Something went wrong");
}
}
});
}
PHP Script
$id = $_POST['postid'];
$type = $_POST['type'];
if($type == "later") {
$sql = "CALL `sp_BlogPosts_PageByPostID`('" . $id ."')";
$exec = mysqli_query($conn, $sql);
while($fetch = mysqli_fetch_assoc($exec)) {
$obj = array(
"PostID" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['PostID']))),
"Title" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['title']))),
"Content" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['content']))),
"Email" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['email']))),
"Username" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['username']))),
"Created_At" => stripslashes(substr(trim(mysqli_real_escape_string($conn, $fetch['created_at'])), 0 , 10)));
echo json_encode($obj);
}
Example Usage
<button onClick="getMoreOlder(1113);"> Load More Posts </button>
Example Return
{"PostID":"1177","Title":"Off track","Content":"Hi This is a Post", "Email":"[email protected]", "Username":"Abc123", "Created_At":"12-2-14"}
Key Points
Correct variables are being sent in POST
Correct information is returned in POST
Always returns error Something went wrong!
Web Inspector shows 200 code for POST request
Upvotes: 0
Views: 79
Reputation: 42746
the problem is you are continually echoing out json_encode, this will make invalid json output.
For example:
{"PostID":"1177","Title":"Off track"}{"PostID":"1177","Title":"Off track"}
This is invalid json. So put all your data into an array/object and then echo json_encode
$objArr = array();
while($fetch = mysqli_fetch_assoc($exec)) {
$objArr[] = array(
"PostID" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['PostID']))),
"Title" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['title']))),
"Content" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['content']))),
"Email" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['email']))),
"Username" => stripslashes(trim(mysqli_real_escape_string($conn, $fetch['username']))),
"Created_At" => stripslashes(substr(trim(mysqli_real_escape_string($conn, $fetch['created_at'])), 0 , 10))
);
}
echo json_encode($objArr);
Or as suggested in the comments fetch all the data at once and echo out that
echo json_encode($exec->fetch_all(MYSQLI_ASSOC));
Also your for loop is using the wrong variable
for (var i=0; i<json.length; i++) {
should be
for (var i=0; i<response.length; i++) {
Upvotes: 2