본문 바로가기
Programming/PHP

php + jquery(ajax) + json 기본

by Berasix 2023. 1. 31.
반응형

javascript 에서는~

<script
  src="https://code.jquery.com/jquery-3.6.3.min.js"
  integrity="sha256-pvPw+upLPUjgMXY0G+8O0xUf+/Im1MZjXxxgOcBQBXU="
  crossorigin="anonymous"></script>
<script language="javascript">
$(document).ready(function($)
{
	initSomething();
});

function initSomething() {
	$.ajax({
		url: './something.php',
		type: "post",
		data: "mode=initSomething",
		dataTyee: "json",
		error : function(error) {
			alert("통신 오류가 발생했습니다!");
		},
		success: function (data) {			
			$('#result').append(data.message);
		}
	});
}

<textarea rows="30" cols="100" id="result" readonly></textarea>

 

php 에서는~(something.php)

<?php
header("Content-Type:application/json");
$mode = $_POST['mode'];

switch( $mode ) {
  case "initSomething" :
    $result['message'] = "어쩌구저쩌구\n";
    echo json_encode($result);
    break;
}
?>

 

728x90

댓글