1

I have this jQuery code which use $.post for Ajax:

$.post(ajax_url , { upload_id: upload_id },
function (response) {
    console.log(response);
    console.log(jQuery.type(response));
}, "json");

and the php code :

$data = Array(
'status' => '1',
'msg' => '',
'progress' => '0.24',
'time' =>'<span class="text-white">06:51</span> left (at 21KB/sec)',
'size' => '<span class="text-white">26KB</span> / 10.91MB (0.24%)');

echo json_encode($data);
die();

and it suppose the console print the json code and the type which is json.
the problem is not print any thing in the console, it means no response.

I put this in the Ajax code to see the error:

.fail( function(jqXHR, textStatus, errorThrown) {
alert(textStatus)});

and the alert was parsererror.

what is the problem ?

UPDATE

If I removed "json" from the ajax code, the response print the type string and the code {"status":1,"msg":"","progress":"0.15","time":"<span class=\"text-white\">11:11<\/span> left (at 10KB\/sec)","size":"<span class=\"text-white\">16KB<\/span> \/ 10.91MB (0.15%)"}

I need it json

Adam Mo.
  • 762
  • 2
  • 11
  • 26

2 Answers2

2

Try this in another page and show in console.

<?php
if($_POST){
header('Content-type:application/json');
$data = Array(
'status' => '1',
'msg' => '',
'progress' => '0.24',
'time' =>'<span class="text-white">06:51</span> left (at 21KB/sec)',
'size' => '<span class="text-white">26KB</span> / 10.91MB (0.24%)');
echo json_encode($data);
exit();
}

?>
<input type="button" onclick="senddata()" value="send" />
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
<script> 
function senddata(){
    var upload_id=1
    $.post('test.php' , { upload_id: upload_id },
    function (response) {
    console.log(response.status);
    }, "json");
} 
</script>
Alfiza malek
  • 994
  • 1
  • 14
  • 36
0

Is your server sending data as Content-Type "*/json"? If not, modify the response headers accordingly. Sending "application/json" would be fine, for example. And the code would be: header('Content-type:application/json');

Another possible solution is by switching '\' to '/' in your json url.

Manikiran
  • 2,618
  • 1
  • 23
  • 39