weixin_33695082 2017-07-11 16:32 采纳率: 0%
浏览 15

如何在jQuery中放置错误

so this is my console log

{
"name": ["The name field is required."],
"type": ["The type field is required."],
"ceiling_amount": ["The ceiling amount field is required."]
}

how can i put errors in my html like this

console.log(res)    
if(res.hasOwnProperty('errors')){
    for(var field in res.errors){
        var fieldname = field;
        var input = $("[name=\""+fieldname+"\"]");
        input.closest('.form-group').addClass('has-error')
        input.after('<span class="help-block">'+res.errors[field]+"</span>")
    }
}else{
    window.location.href = res.redirectTo;
}

but this html i use .done() here

but my question is how can i put errors when i use .fail() and thats my response json?

  • 写回答

1条回答 默认 最新

  • weixin_33709219 2017-07-11 16:41
    关注

    Based on your sample data, there's no error property there, but you can use the .error() function of your ajax. Here's my typical ajax request that I often use:

    $.ajax({
            url: url,
            type: 'post',
            dataType: 'json',
            data: myData,
            error: function (data){
                // here's your error data
                if( data.status == 422){ // this is the status code that laravel 
                                         //  sends when a validation failed: 422
                                         //    (Unprocessable Entity)
                  console.warn(data.responseJSON);
                }else{
                  console.warn("this is some other error.");
                }
            },
            success: function (data){
                // do something; status code = 200
            }
        }).complete(function (response){
            // do something; regardless of the status code
        });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作