2012-04-03 1 views
1

警告があったされていないのはなぜ私はこのJSONレスポンス検証 - アヤックス

{"error":"repeated"} 

を持っている場合は?どこかに置く必要がありますdataType: "json"

$(function() { 
     $("#box a").click(function() { 
      var selectedId = $(this).attr("class"); 

      $.post("new_list.php", { 
       id_user: <?php echo $id;?>, 
       id_list: selectedId 
      }, function (data) { 
        if(data.error == 'repeated') { 
         alert("error"); 
        } 
      }); 
     }); 
    }); 

おかげ

答えて

2

は、あなたがそれを指示しない限り、自動的にJSONとしてそれを解析するつもりはありません。

$(function() { 
    $("#box a").click(function() { 
     var selectedId = $(this).attr("class"); 

     $.post("new_list.php", { 
      id_user: <?php echo $id;?>, 
      id_list: selectedId 
     }, function (data) { 
       if(data.error == 'repeated') { 
        alert("error"); 
       } 
     }, 
     "json"); //Here, add this as last parameter 
    }); 
}); 
1

それはJavascriptのオブジェクトに応答文字列を変換の問題のようになります。あなたは自分の場合は自分で

0

チェックをいくつかの手間を省く、代わりにjQuery.getJSON関数を使用することもできますが

function (data) { 
    // Parse the JSON 
    data = JSON.parse(data); 

    if(data.error == 'repeated') { 
     alert("error"); 
    } 
} 

replyはオブジェクトではなく文字列です。そして、あなたは

0
$(function() { 
     $("#box a").click(function() { 
      var selectedId = $(this).attr("class"); 

      $.post("new_list.php", { 
       id_user: <?php echo $id;?>, 
       id_list: selectedId 
      }, function (data) { 
        if(data.error == 'repeated') { 
         alert("error"); 
        } 
      },dataType: "json"); 
     }); 
    }); 

var obj = jQuery.parseJSON(data); 

を呼び出し、それが「繰り返し」されたかどうかを確認するためにobj.error使用しなければならない次のスニペットは、jQueryのあなたの期待JSONをお知らせします。