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 .

5
  • 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. Commented Jun 23, 2022 at 10:12
  • Uncaught SyntaxError: Unexpected token o in JSON at position 1 at Function.parse [as parseJSON] (<anonymous>) at Object.success (index.php:76:24) this is the error code (index.php:76:24 ) => data = $.parseJSON(data); Commented Jun 23, 2022 at 10:18
  • 1
    i see , when i remove the code , $.parseJSON(data); it worked. Commented Jun 23, 2022 at 10:25
  • 1
    dataType: 'JSON' tells jQuery to parse the response as JSON automatically (you can read that in the documentation). 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. Commented Jun 23, 2022 at 10:41
  • @ADyson love your explanation. Commented Jun 23, 2022 at 10:57

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.