如何删除parsererror:SyntaxError:意外令牌<在JSON中的位置0 [英] how to remove parsererror: SyntaxError: Unexpected token < in JSON at position 0

查看:246
本文介绍了如何删除parsererror:SyntaxError:意外令牌<在JSON中的位置0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用PHP从MySQL数据库获取数据并将其作为JSON传递.当我尝试显示响应时,它显示错误parsererror:SyntaxError:意外的令牌<在位置0的JSON中.有人可以提供帮助. 下面是我的jQuery和PHP代码:

I am trying to fetch data from MySQL Database using PHP and passing it on as JSON. When I try to display the response, it displays the error parsererror: SyntaxError: Unexpected token < in JSON at position 0. Can someone please help. Below is my jQuery and PHP Code:

jQuery:

$(document).ready(function()
{
    $("#display").change(function()
    {
        var type = document.getElementById('display').value;
        alert(type);
        $.ajax(
        {
            //create an ajax request to load_page.php
            type: "GET",
            url: "DBOperations.php",
            data : "type=" +type,
            dataType: "json",  //expect text to be returned                
            success: function(response)
            {                  
                //$("#response").html(response); 
                //alert(response.);

                $.each(response, function(index, element)
                {
                    $('#response').html($(
                    {
                        text: element.name
                    }));
                });
            },
            error: function(jqXHR, textStatus, errorThrown)
            {
                alert('error: ' + textStatus + ': ' + errorThrown);
            }
        });
    });
});

PHP:

try
{
    $dsn = 'mysql:host=localhost;dbname=practice_db'; //your host and database name here.
    $username = 'root';
    $password = '';

    //Connect to database
    $conn = new PDO($dsn, $username, $password);
    $query = "SELECT * FROM client WHERE client_type = :client_type";

    //Prepare and Execute Query
    $stmt = $conn->prepare($query);    
    $stmt->bindParam(':client_type', $type);
    $stmt->execute();
    //echo 'Here: ' .$stmt;
    //$rows = $stmt->fetch();
    $rows = $stmt->fetchAll();
    foreach ($rows as $row)
    {
        echo "ClientID: ".$row['client_id'] . " ";
        echo "Name: ".$row['client_name'] . " ";
        echo "Title: ".$row['client_title'] . " ";
        echo "Client Type: ".$row['client_type'] . "<br>";
    }

    //Display associative array
    echo '<pre>';
    print_r($rows) .'<br>';
    header('Content-type: application/json');
    json_encode($rows);
    print_r(json_encode($rows));
}
catch (PDOException $ex)
{
    echo "There was a problem executing the Query: " . $ex->getMessage();
}

如果我尝试检查使用alert()的响应是什么,它会显示:[object HTMLDivElement]

Further if i try to check what is the response that i am getting using alert(), it shows: [object HTMLDivElement]

推荐答案

从php页面获取json时,仅在页面上打印json.

While fetching json from a php page only print json on the page.

尝试删除此部分

echo'<pre>'; 
print_r($rows).'<br>';

希望这会有所帮助.

这篇关于如何删除parsererror:SyntaxError:意外令牌&lt;在JSON中的位置0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆