Reputation: 1374
I'm writing a web interface for an application. Since the elaboration time for the application can be pretty long, when it starts the user is shown a loading page and then an AJAX call loads the output to the page. If I browse the PHP function with my browser I get the correct response (a JSON), but when the AJAX call is executed jQuery gets an error 500 (I tried with the same parameters).
This is the JavaScript:
$.ajax({
type: "GET",
url: my_url,
dataType: 'json',
success: function(result){
if (result.status == "COMPLETED") {
window.alert("RETURNED");
$("hocrDisplay").attr("src", result.html);
$("hocrDownload").attr("href", resul.path);
$("#loaderImage").hide();
$("#hocrDisplay").show();
$("#hocrDownload").show();
window.alert("The file will be deleted in 10 minutes");
}else{
setTimeout(getStatus(requestid,filename), 3000);
}
},
error: function (response) {
alert("There was an error processing the document");
$("#loaderImage").hide();
}
});
And this is the code around the PHP echo:
echo json_encode('{"status" : "COMPLETED", "html" : "' . $htmlname . '", "path" : "' . $tarpath . '"}');
ob_flush();
sleep(600);
unlink($tarpath);
unlink($htmlname);
Upvotes: 0
Views: 95
Reputation: 22158
This line is wrong:
echo json_encode('{"status" : "COMPLETED", "html" : "' . $htmlname . '", "path" : "' . $tarpath . '"}');
You should make an array and then encode it in JSON like this:
$array = array("status"=>"COMPLETED",
"html"=>$htmlname,
"path"=>$tarpath);
echo json_encode($array);
This encodes you the correct jSON. The 500 error is in server, so that line it that produces the error.
Good luck,
Upvotes: 1
Reputation: 16117
You are encoding the string
which already encoded, its better to use an array
than use json_encode
function for encoding.
You can try like that:
// create an array for your values
$yourArr = array(
'status'=>'COMPLETED',
'html'=>$htmlname,
'path'=>$tarpath);
// encode the array in json format
echo json_encode($yourArr);
json_encode will return this:
{"status":"COMPLETED","html":"test","path":"test2"}
Now, you can use it in ajax
success as you are already doing.
Upvotes: 0