function view_record2()
{
$("#searchbox").keyup(function()
{
var searchword = $(this).val();
$.ajax(
{
url: 'view.php',
method: 'post',
data:{searchword:searchword},
dataType: 'JSON',
success: function(data)
{
data = $.parseJSON(data);
if(data.status=='success')
{
$('#table').html(data.html);
}
}
})
})
}
PHP :
function display_record2()
{
global $con;
$search = $_POST['searchword'];
$value = "";
$value = '<table class="table table-bordered">
<tr>
......
$value.='</table>';
echo json_encode(['status'=>'success','html'=>$value]);
}
When i send data type :text everything is ok , but when i use JSON data type , not working post action.What is the problem with my code? Actually im new to coding so simple mistakes gets very problem to progress .
dataType: 'JSON'tells jQuery to parse the response as JSON automatically (you can read that in the documentation). Sodatawill already be an object. You don't need to parse it again - and doing so fails as you've seen, because an object isn't a string, and JSON.parse obviously expects to receive a string.