2015-10-27 59 views
0

可以幫助我。我的成功功能似乎不起作用。 beforesend正在工作,我檢查了變量s。在ajax之前,所有的驗證都是正確的。請看看..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:[email protected]">[email protected]</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") 
       } 
      } 
     }); 
    } 
} 
+3

發送或傳遞數據有什麼不行?有錯誤消息嗎?你期望發生什麼,實際發生了什麼? – Jasen

+0

打開開發人員工具(使用您正在使用的瀏覽器),切換到網絡選項卡,刷新頁面,使您的功能運行併發布此請求的響應代碼。它是200還是別的? – dchayka

+0

謝謝非常感謝您的回覆。我面臨的問題是ajax正在執行直到發送之前,但成功函數未被執行。我知道這一點,因爲我看到 console.log('happeneing'); 在開發人員工具中,但我看不到 console.log(result); 由於錯誤功能沒有顯示任何錯誤,因此沒有錯誤。在networks選項卡中,我看到名稱mailer.php正在解析的XML數據。 –

回答

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()回調已棄用截至日期jQuery 1.8。要準備的代碼爲他們的最終消除,使用jqXHR.done()jqXHR.fail(),並jqXHR.always()代替。

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的