[jQuery] Ajax

Code/JavaScript 2018. 12. 14. 21:27

    - 콜백 함수
    v3.0 이하 : success(), error(), complete()
    v3.0 이상 : done(), fail(),  always()


//======================

//ajax_jq.htm


<!DOCTYPE html>
<html lang="ko">
<head>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.js"></script>

<script>

function doajax(){

    id = "admin";
    data1 = JSON.stringify( {"id1": id });

    var ajax1 = $.ajax({
        url : "server.php", 
        type : "post",
        data : {q:data1},
        dataType : "json",        
        cache : false //기본 true
        //async : true, //기본 true       
        //contentType : 'application/x-www-form-urlencoded; charset=UTF-8', //기본
        //contentType : 'application/json',
    });
    ajax1.done(function( data, status){
        console.log("SUCCESS!! - Data: " + data + "\nStatus: " +status);
        console.log(data.name , data.time);
    });
    ajax1.fail(function(jqxhr1, status, error){
        console.log("Failed - error",  jqxhr1, status, error);
    });
}

</script>

</head>

<body>

<h2>JQuery Ajax Example</h2>
<input id="input1" type="text" value="ajax1"></input>
<button id="do_ajax" onclick="doajax();">Do Ajax</button>
<p id="status">Ready</p>
<p id="result">Result</p>

</body>
</html>


<script>
/*
    id = "admin";
    data = JSON.stringify( {"id1": id });
    $.get(
        "server.php",  
        data,
        function(data, status){
            console.log("Data: " + data + "\nStatus: " + status);
            console.log(data.name , data.time);
            $("#result").html( data );
            $("#status").text( "Succeeded!" );
        },
        "json")       
        .fail(function(xhr1, status, error){
            console.log("fail - error",  xhr1, status, error);
        });   
}

*/
/*
    id = "admin";
    data = JSON.stringify( {"id1": id });
    $.post(                 //cache 안됨
        "server.php",  
        {q : data},
        function(data, status){
            console.log("Data: " + data + "\nStatus: " + status);
            console.log(data.name , data.time);
            $("#result").html( data );
            $("#status").text( "Succeeded!" );
        },
        "json")       
        .fail(function(xhr1, status, error){
            console.log("fail - error",  xhr1, status, error);
        });   
*/



//=======================================

// 라라벨에서 POST 방식 사용시 주의할점

- CSRF 보안을 위해 토큰 추가 설정을 해야 에러가 나지 않는다.


    $.ajaxSetup({

        headers: {

            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')

        }

    });

//=======================================



//=======================================

//server.php

<?php
//header("Content-Type: application/json; charset=UTF-8");
error_reporting(0);

$rt = $_REQUEST['q'];
$gt = $_GET['q'];
$pt = $_POST['q'];

//echo "서버 응답: "."REQ=[".$rt."] , "."GET=[".$gt."] , "."POST=[".$pt."] "."id2=[".$id2."]";

$jq = json_decode($pt);
$id1 = $jq->id1;
$id2 = $id1."-2";
echo json_encode(array("name"=>$id2, "time"=>"2pm"));

?>




반응형
Posted by codens