회원가입화면에서 ajax를 이용해 id 중복검사를 하려고 합니다.
view에서 id를 입력하면 ajax와 controller, model의 통신으로 중복되면 no, 아니면 ok, 입력한 값이 없다면 ??이라는 결과값을 내야하는데
계속 ??이라는 값만 도출하고 있습니다.
이상한건 controller에서 echo로 도출하는 결과값은 중복확인 결과를 0,1,-1의 값으로 제대로 출력해주고 있습니다.
그러니까 컨트롤러와 ajax가 서로 다른 얘기를 하고 있는 겁니다.
대체 어디서 어떻게 뭘 빠트렸는지 알려주시면 감사하겠습니다.
소스는 아래와 같습니다.
////////////////////////////view/////////////////////
<?php
$attr=array('class'=>'regForm', 'id'=>'regForm');
echo form_open('/ajax_user/register', $attr);?>
<div class="title">회원가입</div>
<div class="mg_btm5">
<label for="user_id" class="ft18">아이디</label>
<input class="ft15 inputBox" type="text" name="user_id" id="user_id">
<p id="check1"></p>
</div>
////////////////////////////////js//////////////////////////////////////////
$(document).ready(function(){
var csrf_token=$('input[name="ci_t"]').val();
var id_ck=RegExp(/^[a-zA-Z]{1}[a-zA-Z0-9_\-]{4,15}$/);
$("#user_id").blur(function(){
if($("#user_id").val()){
if(!id_ck.test($("#user_id").val())){
$("#check1").html("5~15자 영문대소문자,숫자, -_ 가능 (영문자로 시작)");
return false;
}
$.ajax({
url:"/index.php/ajax_user/idcheck",
type:"post",
data:{user_id:$("#user_id").val(),
ci_t:csrf_token
},
success:function(data){
if(data=='0'){
$("#check1").html('ok');
}else if(data=='1'){
$("#check1").html('no');
}else{
$("#check1").html('??');
}
},
error:function(request, status, error){
alert("code:"+request.status+"\n message:"+request.responseText+"\n error"+error);
}
});
}else{
$("#check1").html("아이디를 입력하세요");
}
});
});
//////////////////////controller////////////////////////////////
function idcheck(){
$data=array(
'user_id'=>$this->input->post('user_id', TRUE));
$result=$this->user_m->check_id($data);
if($this->input->post('user_id', TRUE)!=''){
if($result<1){
echo '0';//가능
}else{
echo '1'; //중복
}
}else{
echo '-1';//null
}
}
웅파님 말씀대로 data를 출력해보았습니다.
alert로 data를 출력한 결과입니다. 지금 이상황은 html문서를 다 가져오는...건가요..??
하..............감사합니다 웅파님 배강민님 ..
function __construct(){}에 캐릭터셋 세팅부분과 _remap으로 항상 헤더 푸터가 로드되는 부분이 있어서 그런것 같습니다.
이유를 드디어 찾았네요. 정말 감사드립니다.
하나만 더 여쭙겠습니다. 이럴경우 ajax가 있을경우 _remap 설정을 추가해야되는 건가요?
아니면 _remap을 아예 쓰지 말아야 하나요?
public function _remap($method)
{
$this->segs = $this->uri->segment_array();
if ($this->input->is_ajax_request()) {
//ajax 이면 헤더 푸터 없음
if (method_exists($this, $method)) {
$this -> {"{$method}"}();
}
} else if(isset($this->segs[4]) && $this->segs[4] =="excel"){
//엑셀로 호출 하면
if (method_exists($this, $method)) $this -> {"{$method}"}();
} else {
//ajax가 아니면
$this->load->view("/common/header");
if (method_exists($this, $method)) {
$this->{"{$method}"}();
}
$this->load->view("/common/footer");
//$this->output->enable_profiler(true);
}
}
이런 방법도 있습니다
개인적으로 컨트롤러마다 넣어놓고 사용하고 있지요
ajax , excel 호출도 체크 합니다