2014-04-11 2 views
0

사용자가 jQuery 자동 완성 상자에서 활동 (예 : 영화 또는 합기도 시청)을 선택할 수있는 응용 프로그램이 있습니다. 그 부분이 효과가 있습니다. 그러나 내가 원하는 것은 전문 지식이 관련이없는 경우 (예 : 영화 감상) 또는 전문 지식이 관련되어있는 경우 활성화되도록 사용자가 자신의 전문 지식을 선택하도록 허용하는 일부 상자에 대한 것입니다. 이렇게하려면 autocomplete select 이벤트 내에서 $ .getJSON을 호출하려고합니다. $ .getJSON은 서버 측 php 스크립트를 호출합니다.이 스크립트는 방금 선택한 활동에 전문 지식이 필요한지 여부를 mysql 데이터베이스에 요청합니다. 그러나 .getJSON을 호출하지 않는 것 같습니다.jQuery 자동 완성 서버 측 스크립트 호출

다음
<label for="activity">Select Activity:</label> 
<input id="activity"> 

내 자바 스크립트입니다 :

$("#activity").autocomplete({ 
    minLength: 2, 
    source: "php/getActivities.php", 
    select: function(event, ui) { 

     // Call the server side script to determine whether or not this activity 
     // requires expertise or not 
     queryString = 'activity=' + encodeURIComponent(ui.item.value); 
     alert (queryString); 

     $.getJSON('php/checkActivityExpertise.php',queryString,function(data2) { 

      alert('in .getJSON'); 

      if (data2.needExpertise) { 
       activateBoxes(); 
      } 
      else { 
       deactivateBoxes(); 
      } 

     }); // end of $.getJSON call 
    } // end of select: function 
}); // end of activity.autocomplete 

그리고 여기에 PHP 함수입니다 : 여기

내 HTML입니다

<?php // checkActivityExpertise.php 

// Connect to database 
include_once 'dbConnect.php'; 
include_once 'sanitizeString.php'; 

$activity = sanitizeString($_GET['activity']); 

// query database on whether or not expertise is needed 
$query = "SELECT displayExpertise FROM activities WHERE activity=$activity"; 
$resource = mysql_query($query); 
$row = mysql_fetch_row($resource); 
$needExpertise = $row[0]; 

// Return the flags back to calling function 

$arr = array('needExpertise' => $needExpertise); 

echo json_encode($arr); 

?> 

경고 (queryString이); 작품을 찾을 수 있습니다. 경고 ('.getJSON'); 그러나 작동하지 않습니다. 나는 모든 PHP 함수를 주석 처리하려고했습니다. 그래서 자동 완성 select 이벤트 내부에서 $ .getJSON을 호출 할 수 없는지 궁금합니다.

감사합니다,

답변

1

무엇이 잘못되었는지 알기가 어렵습니다. 다른 상태에 대한 처리기를 추가하는 것이 무엇인지, 특히 fail 케이스 :

$.getJSON('php/checkActivityExpertise.php',queryString,function(data2) { 
    alert('in .getJSON'); 
    if (data2.needExpertise) { 
     activateBoxes(); 
    } 
    else { 
     deactivateBoxes(); 
    } 
}).done(function() { 
    alert("done"); 
}) 
.fail(function(jqxhr, textStatus, error) { 
    var err = textStatus + ", " + error; 
    alert("Request Failed: " + err); 
}) 
.always(function() { 
    alert("always"); 
}).complete(function() { 
    alert("complete"); 
}); 
+0

감사합니다. 내가 그거 했어. 그것은 나에게 오류를주고있다 : "요청 실패 : parsererror, SyntaxError : 예기치 않은 토큰 <"그 오류가 발생하는 이유를 모르십니까? – Tom78

+0

당신의 querystring은 무엇입니까? – Starscream1984

+0

querystring은 "activity = Aikido"입니다 (예 : 활동은 사용자가 텍스트 상자에 입력 한 내용에 따라 다름) – Tom78

0
가 완료 핸들러를 트리거하지 않도록 getJSON() 오류를 반환 할 수있다

- 그래서 당신은 경고를 넣을 수있는 문서

var jqxhr = $.getJSON("example.json", function() { 
    console.log("success"); 
}) 
.done(function() { 
    console.log("second success"); 
}) 
.fail(function() { 
    console.log("error"); 
}) 
.always(function() { 
    console.log("complete"); 
}); 

에서 대신 패턴을 사용 전화의 결과가 무엇인지 확인하십시오