[ Web 관련 ]/jQuery
(CI restAPI) ajax put 전송
BIZLAB
2020. 9. 9. 15:38
javascript
<script>
function send(){
var frm = $("form[name=frm]").serialize() ;
$.ajax({
url:'http://주소', //request 보낼 서버의 경로
type:'put', // 메소드(get, post, put 등)
async: true, //동기: false, 비동기(기본값): true
//dataType: "json",
//data:{'t_name':t_name}, //보낼 데이터,
data:frm, //보낼 데이터,
timeout: 2000, //2초,
contentType:'application/json; charset=utf-8',
headers: {
"Content-type":"application/json; charset=utf-8",
"Authorization":token
},
success: function(data) {
console.log("성공');
console.log(data.status);
},
error: function(request, status, error) {
//서버로부터 응답이 정상적으로 처리되지 못햇을 때 실행
rtnmsg = JSON.parse(request.responseText);
console.log("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
}
});
}
</script>
php
public function targets_edit_put()
{
header("Access-Control-Allow-Origin:*");
$_POST = json_decode($this->security->xss_clean(file_get_contents("php://input")), true);
$this->form_validation->set_data([
'idx' => $this->input->input_stream('idx', TRUE),
'name' => $this->input->input_stream('name', TRUE)
]);
$this->form_validation->set_rules('idx', 'Idx', 'trim|required',
array('required' => '올바른 요청이 아닙니다.')
);
$this->form_validation->set_rules('name', 'Name', 'trim|required|max_length[100]',
array('required' => '필수 항목입니다.')
);
if ($this->form_validation->run() == FALSE)
{
//echo "Error";
$message = array(
'status' => false,
'code' => '000',
'error' => $this->form_validation->error_array(),
'message' => $this->validation_errors()
);
$this->response($message, REST_Controller::HTTP_NOT_FOUND);
}
else
{
echo 'input_stream=>'.$this->input->input_stream('name', TRUE);
}
}