Ajax成功函数似乎不起作用

时间:2015-10-27 22:11:47

标签: javascript jquery ajax

有人可以帮助我。我的成功功能似乎不起作用。 beforesend正在工作,我检查了变量s。在ajax之前确实如此,所有验证都是正确的。请看看..

function enquiry_validations() {
    if (s) {
        var url = "http://localhost:9080/c/wp-content/themes/aussie/mailer.php";
        jQuery.ajax({
            type: "POST",
            url: url,
            data: "property_type=" + property_type + "&bedrooms=" + bedroom + "&bathroom=" + bathroom + "&condition=" + condition + "&est_size=" + est + "&parking=" + packing + "&special_feature=" + spl_fet + "&other=" + oth + "&unit_no=" + unt_no + "&street_no=" + street_no + "&street_name=" + street_name + "&studio=" + suburb + "&State=" + state + "&relation=" + relationship + "&purpose=" + purpose + "&cell=" + time_to_cell + "&currently_listed_n=" + currently_listed + "&first_name=" + first_name + "&sur_name=" + last_name + "&telephone=" + telephone + "&email=" + email,

            error: function (data) {
                console.log(data);

            },
            beforeSend: function () {
                console.log('happeneing');
                jQuery('#ajax-loader').show();
                jQuery('#ajax-loader').html('<img src="http://localhost:9080/c/wp-content/themes/aussie/images/ajax-loader.gif">');
            },
            success: function (result) {
                jQuery('#ajax-loader').hide();
                console.log(result);

                if (result == '0') {
                    console.log("No Result")
                }
                if (result == '1') {
                    jQuery.fancybox('<div class="aussi-en-pop"><h3>Thank you for using Northern Property Reports.</h3> <p>Your Northern Reports representative is busy getting your Property Report ready and will be in touch within 48 hours with your free report. <br> All enquiries please email :  <a href="mailto:info@northernpropertyreports.com.au">info@northernpropertyreports.com.au</a></p></div>');
                    jQuery("#Property_type").val('');
                    jQuery('#bedrooms').val('');
                    jQuery('#bathrooms').val('');
                    jQuery('#condition').val('');
                    jQuery('#est').val('');
                    jQuery('#parking').val('');
                    jQuery("input[type='checkbox']#chk:checked").prop('checked', false);
                    jQuery('#oth').val('');
                    jQuery('#un_no').val('');
                    jQuery('#Street_no').val('');
                    jQuery('#street_name').val('');
                    jQuery('#suburb').val('');
                    jQuery('#state').val('');
                    jQuery('#relationship_to_Property').val('');
                    jQuery('#purpose_of_request').val('');
                    jQuery('#time_to_sell').val('');
                    jQuery("input[type='radio']:checked").prop('checked', false);;
                    jQuery('#first_name').val('');
                    jQuery('#last_name').val('');
                    jQuery('#telephone').val('');
                    jQuery('#email').val('');
                    jQuery('#confirm_email').val('');
                    jQuery("input[type='checkbox']#agree:checked").prop('checked', false);
                    console.log("YES Result")
                }
            }
        });
    }
}

2 个答案:

答案 0 :(得分:1)

也许发布你的PHP脚本会更好地帮助我们理解这个问题,但对于初学者来说......

我注意到了这一点:

data: "property_type=" + property_type + "&bedrooms=" + bedroom + "&bathroom=" + bathroom + "&condition=" + condition + "&est_size=" + est + "&parking=" + packing + "&special_feature=" + spl_fet + "&other=" + oth + "&unit_no=" + unt_no + "&street_no=" + street_no + "&street_name=" + street_name + "&studio=" + suburb + "&State=" + state + "&relation=" + relationship + "&purpose=" + purpose + "&cell=" + time_to_cell + "&currently_listed_n=" + currently_listed + "&first_name=" + first_name + "&sur_name=" + last_name + "&telephone=" + telephone + "&email=" + email,

我可以建议:

data: {
        'data': {
            'property_type': property_type,
            'bedrooms': bedroom,
            'bathroom': bathroom,
            'condition': condition
         }
},

在PHP脚本上:

<?php
   $data = array();
   $response = array();

   if(isset($_POST['data']) {
       $data = $_POST['data'];
       $response['code'] = "200";
       $response['message'] = "Hi! The property type is " . $data['property_type'];
   }

  echo json_encode($response);

success函数上:

success: function( response ) {
    if(response.message) {
        console.log(response.message);
    }
}

此外,来自jQuery.ajax() API documentation

  

弃用通知 jqXHR.success() jqXHR.error() jqXHR.complete() < / strong>回调在 jQuery 1.8 之后已弃用。要准备最终删除的代码,请使用 jqXHR.done() jqXHR.fail() jqXHR.always() 代替

答案 1 :(得分:0)

data: {"property_type": property_type ,
          "bedrooms": bedroom,
          "bathroom" : bathroom ,
          "condition" : condition ,
          "est_size" : est ,
          "parking" : packing ,
          "special_feature" : spl_fet ,
          "other" : oth ,
          "unit_no" : unt_no ,
          "street_no" : street_no ,
          "street_name" : street_name ,
          "studio" : suburb ,
          "State" : state ,
          "relation" : relationship ,
          "purpose" : purpose ,
          "cell" : time_to_cell ,
          "currently_listed_n" : currently_listed ,
          "first_name" : first_name ,
          "sur_name" : last_name ,
          "telephone" : telephone ,
          "email" : email}

使用此格式从jquery发送或传递数据