0

コンセプトの入力フィールドがあり、ユーザーが入力すると、コンセプトが存在するかどうかを確認する必要があります。そこで、ajax と JavaScript を使用してデータベースをチェックし、概念が存在するかどうかを確認するチェック ボタンを作成しました。私の問題は、ajax と JavaScript を使用すると、次の例外が発生することです。

入力の予期しない終了

JS:

var concept = document.getElementById('acConceptName').value;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4 && xmlhttp.status==200){
            var isexisted = JSON.parse(xmlhttp.responseText);
            if(isexisted[0]==true){
                var errorMessage = document.getElementById('acSuggesConcepts');
                var p = document.createElement('p');
                p.innerHTML="this concept is already existed";
                errorMessage.appendChild(p);
                errorMessage.style.display="block";            
            }
        }
    }
    xmlhttp.open("GET","http://localhost/Mar7ba/Ontology/isExistedConcept/"+concept+"/TRUE",true);
    xmlhttp.send();

例外とは何ですか?どうすれば解決できますか?

PHP:データベースをチェックする関数で、常にtrueを返します

public function isExistedConcept($concpetName,$Ajax){
        if($Ajax==true){
            $results=true
             $d=array($results);
            return json_encode($d);
        }
 }

デモ: http://jsfiddle.net/Wiliam_Kinaan/s7Srx/2/

4

3 に答える 3

2

しばらくコードを見た後、容疑者と思われるものの 1 つに PHP があります。

PHP の関数はreturnコマンドで終了します。AJAX 呼び出しが実際に待っているのは、送り返されるデータです。return コマンドは、その値を最初に関数を呼び出したエンティティに戻すだけです。

関数をecho返すのではなく、結果に変更してみてください。クライアントにデータを返す場合ではなく、結果を別の PHP 関数に渡す必要がある場合に備えて、戻り値を保存します。
ここでは、読みやすくするために、この return コマンドのみを記載しています。

public function isExistedConcept($concpetName,$Ajax){
  if($Ajax==true){
    $results=true
    $d=array($results);
    echo json_encode($d);
  }
  return;
 }
于 2012-05-13T19:40:26.360 に答える
0

これを試して:

public function isExistedConcept($concpetName,$Ajax) {
    if( $Ajax) return "1";
}
// This is a simplified version of what you're doing, but it returns "1" instead of "[true]"
// Now for the JS:

if( xmlhttp.readyState == 4 && xmlhttp.status == 200) {
    var isexisted = xmlhttp.responseText == "1";
    if( isexisted) {...}

それでもうまくいかない場合は、追加alert(xmlhttp.responseText)してみて、あるべきもの以外のものを取得しているかどうかを確認してください。

于 2012-05-13T18:41:57.510 に答える
0

これを試して :

var concept = document.getElementById('acConceptName').value;
    xmlhttp = new XMLHttpRequest();
    xmlhttp.open("GET","http://localhost/Mar7ba/Ontology/isExistedConcept/"+concept+"/TRUE",true);
    xmlhttp.onreadystatechange=function(){
        if(xmlhttp.readyState==4){
            if(xmlhttp.status==200){
                var isexisted = JSON.parse(xmlhttp.responseText);
                if(isexisted[0]==true){
                    var errorMessage = document.getElementById('acSuggesConcepts');
                    var p = document.createElement('p');
                    p.innerHTML="this concept is already existed";
                    errorMessage.appendChild(p);
                    errorMessage.style.display="block";            
                }
                    else{ 
                        console.log('error');
                    }
            }
        }
    }
    xmlhttp.send(null);
于 2012-05-13T18:49:12.080 に答える