Ajax JSON无效字符

时间:2016-04-13 12:47:36

标签: jquery json ajax

解析我的JSON时遇到问题。当我解析对JSON的响应时,我收到错误。该错误表明存在无效字符。我不知道如何解决这个问题。每次我得到同样的错误。

截图

screenshot

PHP

case 'todobyperson':
    switch($_SERVER['REQUEST_METHOD']){
        case 'GET':
            // Get all todo's by person
            $person_id = explode('/',$_SERVER['PATH_INFO']);
            if (isset($person_id[2])) {
                $person_id = $person_id[2];
            }
            try {
                $sql = "SELECT * FROM todo WHERE person_id=:person_id AND done = 0";
                $params = array(':person_id'=>$person_id);
                $stmt = $db->prepare($sql);

                if ($stmt->execute($params) !== false) {
                    echo json_encode($stmt->fetchAll(PDO::FETCH_ASSOC));
                } else {
                    // return false;
                    echo json_encode("False");
                }
            }  catch(PDOException $e){
                json_encode(var_dump($e->getMessage()));
            }
    }
    break;

JS

function loadTodos(page) {
    console.log('loading todos')
        // get Todos
    if (online) {
        $.ajax({
            url: '/backend/handle-request.php/todobyperson/' + JSON.parse(localStorage.getItem('person')).id,
            type: 'GET',
            datatype: "application/json"
        }).done(function(response) {
            localStorage.setItem('todos', response);
            console.log("res " + response);
            response = $.parseJSON(response);

            console.log("na " + response);

            buildTodos(response, page);
        })
    } else {
        response = JSON.parse(localStorage.getItem('todos'));
        buildTodos(response, page);
    }
}

function buildTodos(response, page) {
    var datalist = '<ul id="todolist" data-role="listview">';
    console.log("con " + response);

    response.forEach(function(item, index) {
        datalist += '<li>' + item.title;
        datalist += '<p class="ui-li-aside">Due by <strong>' + item.due_date + '</strong></p>';
        datalist += '<div class="detail"><p>' + item.description + '</p><a data-id="' + item.id + '" href="#" class="done ui-btn ui-btn-inline">Done!</a></div>';
        datalist += '</li>';
    });
    datalist += '</ul>';
    $(page).find('#todos1').html(datalist).enhanceWithin();
}

0 个答案:

没有答案