来自php服务器的jquery返回错误消息未显示在div中

来自php服务器的jquery返回错误消息未显示在div中,php,jquery,post,Php,Jquery,Post,我有一个简单的js代码,如果出现诸如custom msg之类的错误,我想从我的php json服务器端返回错误msg php 我正试图让我的#cardlist div从我的服务器端接收此错误消息'fail to insert'。它不会在div中显示msg,而是在数据正确填充时显示 更新为format()函数 函数格式(d){ var html=null; var card_os=null; $。每个(d,函数(k,v){ 如果(v.card_os){ card_os=v.card_os; } 如

我有一个简单的js代码,如果出现诸如custom msg之类的错误,我想从我的php json服务器端返回错误msg

php

我正试图让我的#cardlist div从我的服务器端接收此错误消息'fail to insert'。它不会在div中显示msg,而是在数据正确填充时显示

更新为format()函数

函数格式(d){
var html=null;
var card_os=null;
$。每个(d,函数(k,v){
如果(v.card_os){
card_os=v.card_os;
}
如果(v.card_id){
var card\u id=v.card\u id;
html+='
  • '+ ''+卡id+''+ ''; } }); $('test_title').html('test:'+card_os+''); 返回html; }
  • html代码

    <div id="columns" class="grid-4" style="display:block;">
            <ul class="test columns-4">
            <div id="cardlist"></div>
            </ul>
        </div>
    
    
    

    您也可以显示您的html代码吗?可能您的div id错误,或者它是一个类,您使用了错误的标记

    您也可以显示您的html代码吗?可能您的div id错误,或者它是一个类,您使用了错误的标记

    尝试记录您的
    msg
    所包含的内容。my msg正确记录并转到“fail”部分,但就像my#cardlist div中没有显示错误消息一样。您应该使用
    boolean
    值。仍然相同,在cardlist div上没有附加消息或html的地方没有区别
    格式化(msg)
    做什么?注意
    $('#cardlist').html(格式(msg))尝试记录您的
    msg
    包含的内容。我的msg正确记录并转到“失败”部分,但就像没有在我的#cardlist div中显示错误msg一样。您应该使用
    布尔值来代替。仍然相同,没有区别,在cardlist div中消息不会被追加或html什么
    格式化(msg)
    做什么?注意
    $('#cardlist').html(格式(msg))尝试在浏览器的开发人员控制台中键入$(“#cardlist”).html(“测试”),查看您的div是否得到更新。对象{0:,长度:1,上下文:HTMLDocument→ test.php,选择器:“#cardlist”}中似乎没有任何内容,#cardlist中的文本是否更改为“testing”?我的#cardlist div最初是空的,这不会在div中添加测试,也不会在div的顶部创建另一个div,这一次该div可以尝试键入$(“#cardlist”).html(“testing”)在浏览器的开发人员控制台中,查看div是否得到更新。对象{0:,长度:1,上下文:HTMLDocument→ test.php,选择器:“#cardlist”}中似乎没有任何内容,#cardlist中的文本是否更改为“testing”?我的#cardlist div最初是空的,这不会在div中添加测试,也不会在其上创建另一个div,这次它对该div有效
    
     $(document).ready(function() {
     $.post('./get_cards.php',$(this).serialize(),function(msg){
    
    
     if(msg.status == 'success'){
     $('#cardlist').html(format(msg));                        
     }
     else if(msg.status == 'fail') {
      console.log(msg.error);
     $('#cardlist').html(msg.error);
     }
     },'json');
    
     } );
    
     function format ( d ) {
     var html = null;
     var card_os = null;
     $.each(d,function(k,v){
     if(v.card_os){
      card_os = v.card_os;
      }
     if(v.card_id){
     var card_id = v.card_id;
      html += '<li>'+
              '<h3>'+card_id+'</h3>'+
              '<img src="images/'+card_id+'.jpg" width="100" height="100"'+
              '</li>';
       }
     });
     $('#test_title').html('<h1>test: '+card_os+'</h1>');
     return html;
     }
    
    <div id="columns" class="grid-4" style="display:block;">
            <ul class="test columns-4">
            <div id="cardlist"></div>
            </ul>
        </div>