如何处理Ajax resonseText与条件 [英] how to handle ajax resonseText with conditions

查看:115
本文介绍了如何处理Ajax resonseText与条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了我的表来发表与阿贾克斯,和一切运作良好。至于阿贾克斯而言,它处理它的工作成功地与一个responseText的。但是,这并不意味着一切都在我的情况下,正确处理。

AJAX是张贴我的形式运行PHP文件将数据插入到我的数据库。成功还是失败,都会有一个responseText的,要么说:谢谢你,如果所有的数据插入到数据库,或错误:如果它失败了。

我如何做一个条件,如果从responseText的处理之类的自定义responseText的错误?或者,我可以迫使它在的responseText返回.ajaxError()错误:?

HTML表单:

 <形式方法=邮报行动=contact.phpID =联系>
    <字段集>
        <输入类型=文本ID =电子邮件级=需要通过电子邮件将NAME =电子邮件最大长度=30值=youremail @ example.com/>
        <输入类型=文本ID =主题类=所需的名称=主题最大长度=24值=请输入您的主题/>
        < textarea的ID =味精级=需要textarea的NAME =味精COLS =30行=5>联系原因< / textarea的>
        <输入类型=提交级=formBtn值=冲我发一封电子邮件/>
    < /字段集>
< /形式GT;
 

JQuery的:

  $(窗口).load(函数(){
  $('#'接触)。当作ajaxForm({
        beforeSubmit:formValidate,
        成功:showResponse方法,
        clearForm:真
    });
});
功能formValidate(){
    $('#'接触)。验证({
                规则:{
                    电子邮件:{要求:真实,电子邮件:真实},
                    主题:{要求:真实,MINLENGTH:5},
                    消息:{要求:真实,MINLENGTH:50}
                },
                消息:{
                    电子邮件:'',
                    学科: '',
                    味精:''
                },
                invalidHandler:函数(){
                    $('div.error')隐藏()。
                },
                focusInvalid:真正的,
                onfocusout在:假的,
                submitHandler:函数(){
                    $('#'接触)ajaxSubmit会();
               }
            });
}
功能showResponse方法(responseText的,状态文本,XHR,$形式){
    执行console.log(responseText的);
//警报(responseText的);
    // $(#联系人)进行切换('慢');
}
 

解决方案

我会建议加入数据类型:JSON属性设置为您 $ .ajaxForm 通话。然后,在你的PHP,而不是返回'谢谢'就可以返回:

 返回JSON(阵列('地位'=>'成功'));
 

 返回JSON(阵列('地位'=>'失败'));
 

然后,在显示响应,您可以运行这样的:

 如果(responseText.status ==='失败'){//做一些事情})
 

I have gotten my form to post with ajax, and all works well. As far as ajax is concerned, it processed its job successfully with a responseText. But that doesn't mean that everything processed correctly in my case.

ajax is posting my form which runs a php file to insert data into my database. Success or fail, there will be a responseText that either says: "Thank you" if all data was inserted into db, or "Err:" if it failed.

How do I make an if condition from that responseText to handle that kind of custom responseText error? Or can I force it to return an .ajaxError() upon the responseText of "Err:"?

HTML form:

<form method="post" action="contact.php" id="contact">
    <fieldset>
        <input type="text" id="email" class="required email" name="email"  maxlength="30" value="youremail @ example.com" />
        <input type="text" id="subject" class="required" name="subject" maxlength="24" value="Enter your subject" />
        <textarea id="msg" class="required textarea" name="msg" cols="30" rows="5">Reason for contact.</textarea>
        <input type="submit" class="formBtn" value="Punch me an Email" />
    </fieldset>
</form>

JQuery:

$(window).load(function(){
  $('#contact').ajaxForm({
        beforeSubmit:formValidate, 
        success: showResponse,
        clearForm:true
    }); 
});
function formValidate(){
    $('#contact').validate({
                rules:{
                    email:{required: true, email:true},
                    subject:{required: true, minlength: 5},
                    msg:{required: true, minlength: 50}
                },
                messages:{
                    email:'',
                    subject: '',
                    msg: ''
                },
                invalidHandler:function(){
                    $('div.error').hide();
                },
                focusInvalid:true,
                onfocusout:false,
                submitHandler: function() {
                    $('#contact').ajaxSubmit();
               }
            });
}
function showResponse(responseText,statusText,xhr,$form){
    console.log(responseText);
//    alert(responseText);
    // $("#contact").toggle('slow');
}

解决方案

I would suggest adding the dataType: 'json' property to your $.ajaxForm call. Then in your php instead of returning 'Thanks' you can return:

return json(array('status'=>'success'));

or

return json(array('status'=>'failed'));

Then in show-response you can run something like :

if(responseText.status === 'failed') { // do something })

这篇关于如何处理Ajax resonseText与条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆