2016-06-01 4 views
0

저는 AJAX를 처음 접했으므로 튜토리얼을 발견하고 PHP 코드에 맞도록 해킹했습니다. 그것은 "좋아"버튼 자습서와 코드 내에서 괜찮아요 ""같은 총을 추가하여 데이터베이스에 작동합니다.AJAX 응답을받지 못합니다.

문제는 '좋아요'를 클릭하면 좋아요 수가 페이지에서 업데이트되지 않는다는 것입니다.

원본 튜토리얼 코드는 정상적으로 작동하므로 분명히 나는 ​​어리석은 짓을했다.

여기에 index.php를

function cwRating(id,type,target){ 
$.ajax({ 
    type:'POST', 
    url:'rating.php', 
    data:'bt_id='+id+'&type='+type, 
    success:function(msg){ 
     if(msg == 'err'){ 
      alert('Some problem occured, please try again.'); 
     }else{ 
      $('#'+target).html(msg); 
     } 
    } 
}); 
} 

내 코드입니다 그리고 그것은 등을 보여주고 같은 계산 곳은 장소입니다 :

<span class="like" onClick="cwRating(<?php echo $row_bt['bt_id']; ?>,1,'like_count<?php echo $row_bt['bt_id']; ?>')"></span> 

<span class="counter" id="like_count<?php echo $row_bt['bt_id']; ?>"><?php echo $row_bt['like_num']; ?></span> 

이 전체 rating.php 페이지입니다 :

include_once("likes.php"); 
$likes = new Likes(); 
if($_POST['bt_id']){ 
//previous tutorial data 
$prev_record = $likes->get_rows($_POST['bt_id']); 
//previous total likes 
$prev_like = $prev_record['like_num']; 

//calculates the numbers of like or dislike 
if($_POST['type'] == 1){ 
    $like = ($prev_like + 1); 
    $return_count = $like; 
}else{ 
    $like = $prev_like; 
} 

//store update data 
$data = array('like_num'=>$like,'like_date'=>date("Y-m-d H:i:s")); 
//update condition 
$condition = array('bt_id'=>$_POST['bt_id']); 
//update tutorial like dislike 
$update = $likes->update($data,$condition); 

//return like or dislike number if update is successful, otherwise return error 
echo $update?$return_count:'err'; 
} 

.counter 클래스에서 응답을 받고 싶습니다. 조정이 필요한 AJAX 코드라고 가정합니다.

+0

ajax 호출에서 오류 반환을 시도하십시오. success : function() {}, 오류 : function (err) {console.log ('error', err); }' –

+0

나는 그것을 추가했지만 아무것도 얻지 못했다. – Ironank

+0

성공 함수의 반환을 게시하십시오. 시도해보십시오. 성공한 경우 : function (msg) {console.log (msg); }' –

답변

0

아마 PHP 코드에서 오류가 발생했습니다. Google 크롬을 사용하여 확인하십시오.

1. Right click on your page and then select inspect element; 
2. Click on the "Network" tab; 
3. Find and click on your ajax request in "Name Path" column (rating.php); 
4. Go to "Response" tab; 
5. See the response of your ajax request. 
+0

불행히도 응답 탭에는 아무 것도 없습니다. – Ironank

+0

"머리글"탭으로 이동하십시오. 일반 주제에는 필드 상태 코드가 있습니다.이 값은 무엇입니까? –

0

해결! Chrome에서 '네트워크'탭을 확인하고 몇 가지 오류가 발생했습니다. 주로 'xmlhttprequest는'액세스 제어 허용 기본 경로 '를로드 할 수 없습니다. 내 AJAX 게시 URL에서 나는 www를 삭제했다. 도메인에서 모든 것이 작동합니다.

관련 문제