0
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 .

  • It's likely that the response you're getting back isn't valid JSON, given your description. Open devtools in your browser and inspect the response to see what it contains. – Rory McCrossan Jun 23 '22 at 10:12
  • Uncaught SyntaxError: Unexpected token o in JSON at position 1 at Function.parse [as parseJSON] () at Object.success (index.php:76:24) this is the error code (index.php:76:24 ) => data = $.parseJSON(data); – Gökhan Gokfen Jun 23 '22 at 10:18
  • 1
    i see , when i remove the code , $.parseJSON(data); it worked. – Gökhan Gokfen Jun 23 '22 at 10:25
  • 1
    `dataType: 'JSON'` tells jQuery to parse the response as JSON automatically (you can read that in the [documentation](https://api.jquery.com/jquery.ajax/)). So `data` will 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. – ADyson Jun 23 '22 at 10:41
  • @ADyson love your explanation. – gsharew Jun 23 '22 at 10:57

0 Answers0