CI 묻고 답하기

제목 댓글 ajax code:500 error:Internal Server Error
카테고리 CI 2, 3
글쓴이 강동원 작성시각 2016/12/11 18:39:31
댓글 : 3 추천 : 0 스크랩 : 0 조회수 : 19763   RSS
 	// Controller
 	function comment() {

		$ajax_data = array(
			'table' => $this->input->post('table'),
			'p_description' => $this->input->post('txt_comment'),
			'idx' => $this->input->post('idx')
		);

		$this->punch_m->comment_write($ajax_data);

		$comment['list'] = $this->punch_m->comment_list($ajax_data['idx']);

		echo json_encode($comment['list']);
	}

 

    // Model
    function comment_write($data) {
        $data_array = array(
            'p_description' => $data['p_description'],
            'member_id' => $this->session->userdata('member_id'),
            'member_name' => $this->session->userdata('member_name'),
            'p_id' => $data['idx']
        );

        $this->db->set('p_reg_date', 'now()', false);
        $this->db->insert($data['table'], $data_array);
    }

    function comment_list($parent_idx = '') {
        
        $sql = "select * from punch where p_id='".$parent_idx."'";

        $res = $this->db->query($sql);

        $rows = $res->result();

        return $rows;
    }

 

			$.ajax({
				type: 'POST',
				dataType: 'JSON',
				data: {
					'table'				: table,
					'idx'				: idx,
					'page'				: page,
					'txt_comment'		: txt_comment,
					'member_id'			: member_id,
					'member_name'		: member_name,
					'<?php echo $this->security->get_csrf_token_name(); ?>' : '<?php echo $this->security->get_csrf_hash(); ?>'	
				},
				
				url: '<?php echo base_url();?>index.php/punch/comment',
				async : true,
				contentType:"application/json",
				success: function(result) {
					if (result !== '') {
						alert("success");
						console.log(result);
						//$('.wrap_list_cmt').html(post_data['txt_comment']);
					} else {
						alert('Server error');
					}
				},
				error: function(request, status, error){
	        		alert("code:"+request.status+"\n"+"\n"+"error:"+error);
	        		//alert("error");
	        		console.log("code:"+request.status+"\n"+"\n"+"error:"+error);
	       		},
	       		complete: function(result) {
	       			//$('#txt_comment').val();
	       			//alert(result);
	       		}
			});

안녕하세요. 이번에 새로이 입분한 초보입니다.

 

나름 ajax로 댓글넣는 것을 구현 중인데  code:500 error:Internal Server Error이놈때문에 고생 중입니다. ㅠㅠ

위와 같은 코드로 데이터 통신을 시도할 시에는 500에러가 뜨고 

 

contentType 를 주석 처리하면 error:SyntaxError: Unexpected token < in JSON at position 8199 에러가 뜹니다.. ㅠㅠ

 

ajax 구문 중 dataType 부분과 contentType 부분을 주석처리한 후 통신을 시도하면 데이터를 주고 받기는 받습니다. 그런데 받아온 데이터 뒤로 <html>..................</html> 과같은 값이 주고 받은 데이터 뒤에 붙어서 나옵니다.

{"p_idx":"406","p_id":"2","p_p_no":"0","p_mc_pkg":"","p_tag_no":"","p_responsible":"","p_mc_responsible":"","p_skid":"","p_code":"","p_priority":"0","p_status":"0","p_clear_date":"0000-00-00 00:00:00","p_verified_date":"0000-00-00 00:00:00","p_p_id":"","p_wiring":"","p_description":"\u3147\u3147\u3147","p_materials":"","p_worker":"","p_reg_date":"2016-12-11 19:11:35","member_id":"knut9900848","member_name":"Steve Yoo"}]<!DOCTYPE html>
<html lang="kr">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../favicon.ico">
.....................
.....................
</html>

 

이것 저것 일주일간 검색을 해보고 시도를 해서 데이터 주고 받는 것 까지 성공을 했는데 여기서 막히네요 ㅠㅠ 혹시 위 코드에서 문제되는 부분이 있으면 지적 부탁드립니다 (__ )

 다음글 session_id 출력 (3)
 이전글 {elapsed_time} (3)

댓글

변종원(웅파) / 2016/12/11 20:45:01 / 추천 1

뒤에 html이 붙어서 출력되니까 json으로 인식 못해서 나오는 에러입니다.

컨트롤러내에 remap에서 뷰를 호출하고 있는지 보세요. 만약 호출하고 있으면 ajax 호출일 경우는 remap에서 html을 자동으로 붙이지 않도록 해야 합니다.

강동원 / 2016/12/11 21:00:39 / 추천 0

고맙습니다. 그 생각을 못햇네요. 그리고 한빛에서 몇일전 플레이스토어에 있는 책 2쇄로 업데이트 해 줘서 잘 쓰고 있습니다.

감사합니다.

강동원 / 2016/12/11 21:02:53 / 추천 0
이런 이유로 종원님 책에는 comment Controller을 따로 빼셨군요. 감사합니다.