$.ajax({ type:"post", datatype:"JSON", url:"produce.php?c=activitymanage&a=joinerInfo&aid="+aid+"&appid="+appid, success:function(msg){ var data = eval("(" + msg + ")"); //这句话解决了json返回数据为undefine的情况 var err_code = data[0].err_code; var err_msg = data[0].err_msg; var joinerInfo = data[0].data; if( err_code == 0 ) { var str=''; var len = joinerInfo.length; for( var i=0; i<len;i++) { if(i%2 ==0){str += '<tr class="odd">';} else{str += '<tr class="even">';} str += '<td class="sorting_1"> <input type="checkbox" attr_aid="'+joinerInfo[i].aid+'"/></td> '; str += '<td>'+joinerInfo[i].name+'</td>'; str += '<td>'+joinerInfo[i].tel+'</td>'; str += '<td>'+getDate(joinerInfo[i].time)+'</td>'; str += '<td>'+joinerInfo[i].message+'</td>'; str += '<td>'+joinerInfo[i].company+'</td>'; str+= '</tr>'; } $("#joinInfo").html(str); }else{ $("#joinInfo").html(''); alert(err_msg); return false; } }, error:function(){ alert("网络请求失败,请重试!"); } })
提交的表单也可以序列化之后更方便简洁
$("#addActivityCategory_dialog").dialog({ autoOpen: false, modal: true, 400, height:250, buttons: { 确定: function () { //序列化表单 var formData = $("#addActivityCategory_form").serialize(); if($("#activityCategoryName").val() == ""){ alert("分类名称不能为空!"); return false; } //console.log(formData);return false; //发起ajax请求 $.ajax({ type:'post', url: 'produce.php?c=activity&a=addActivityCategory', dataType: "json", data:formData, processData:true, success:function(msg){ var data = eval(msg); var err_code = data[0].err_code; var err_msg = data[0].err_msg; var category = data[0].data; if(err_code == 0){ alert(err_msg); $("<option value='"+category.id+"'>"+category.name+"</option>").appendTo("#activityCategory_select") //window.location.reload(); }else{ alert(err_msg); return false; /*$.each(msg.errors,function(k,v){ $('label[for='+k+']').append('<span class="error">'+v+'</span>'); });*/ } }, error:function(){ alert("请求失败,请重新发送请求!"); return false; } }); $(this).dialog("close"); }, 取消: function () { $(this).dialog("close"); } }, open: function () { }, close: function () { } });