0

json と ajax を使用して PHP ファイルからデータを取得する方法がわかりません。お願い助けて。

私は試した:

addnews.tpl:

    $("#send").click(function(e){
    e.preventDefault();
    $.ajax({
        url: "news.php?addnews",
        type: "POST",
        data: {
            title: $("#title").val(),
            text: $("#text").val()
        },
        beforeSend: function() {
            $(".loading").css({
                "display": "inline"
            });
        },
        success: function() {

        }
    }); 
});

$("#send").click(function () {
    $.ajax({
        type: 'GET',
        url: "news.php?addnews",
        data: {},
        dataType: 'json',
        success: function(data) {
            $('.message').html(data.error);
        },
        error: function(xhr){
            $('.message').html('error fetching data');
        }
    }); 
}); 

news.php:

    if(isset($_GET['addnews'])) {
    if(!isset($_SESSION['id'])) {
        echo json_encode(array("error" => "error."));
    }
    $news->addNews($_SESSION['id'], $_POST['title'], $_POST['text']);
    $smarty->display("template/addnews.tpl");       
}

別の質問: ページで json 配列 (テキスト) を非表示にする方法は?

4

3 に答える 3