2015-10-23 495 views
-1

我在通過Jquery/Ajax調用API時遇到問題.. 我得到405錯誤... 任何人都可以請修復我的代碼,所以我可以使它可行。請我真的需要它。Api 405方法不允許jquery

錯誤我得到:

enter image description here

這裏是我的代碼:

<script> 
    jQuery.noConflict(); 
    (function($) { 
     $(function() { 
     $("#register_form").submit(function(){ 
      console.log("Wellness API Initiated!"); 

      var api_user_name = $("#user_name").val(); 
      var api_user_email = $("#user_email").val(); 
      var api_payment_first_name = $("#payment_first_name").val(); 
      var api_payment_last_name = $("#payment_last_name").val(); 
      var api_user_password = $("#user_password").val(); 
      var api_client_address_one = $("#client_address_one").val(); 
      var api_client_address_two = $("#client_address_two").val(); 
      var api_client_city = $("#client_city").val(); 
      var api_client_state = $("#client_state").val(); 
      var api_client_zip = $("#client_zip").val(); 
      var api_client_gender = $("#client_gender").val(); 
      var api_client_home_phone = $("#client_home_phone").val(); 
      var api_client_cell_phone = $("#client_cell_phone").val(); 
      var api_client_work_phone = $("#client_work_phone").val(); 
      var api_client_birth_date = $("#client_birth_date").val(); 
      //alert(api_payment_first_name + api_user_name + api_client_zip); 
      $.ajax({ 
       type: "POST", 
       url: "https://api.MYEWELLNESS.com/api/v1/services/membership/create", 
       headers: { 
        'Authorization':'Basic VitalAlert:5kpN9cYTafnVmZTS', 
        'Content-Type':'application/json' 
       }, 
       data: "user_name="+api_user_name+"&password="+api_user_password+"&first_name="+api_payment_first_name+"&last_name="+api_payment_last_name+"&address1="+api_client_address_one+"&address2="+api_client_address_two+"&city="+api_client_city+"&state="+api_client_state+"&country=US&zip="+api_client_zip+"&email="+api_user_email+"&phone="+api_client_home_phone+"&work_phone="+api_client_work_phone+"&cell_phone="+api_client_cell_phone+"&gender="+api_client_gender+"&birthdate="+api_client_birth_date, 
       datatype: "json",/* 
       beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic " +"Vml0YWxhbGVydA==:MTI5OTky"); },*/ 
       success: function(msg){ 
        console.log("Success: "+msg); 
        alert("success"); 
        alert(data); 
        return flase; 
       }, 
       error: function(error){ 
        console.log("Failed: "+error); 
        alert("false"); 
        alert(data); 

        return false; 
       } 
      }); 


      console.log("API Fire Complete!"); 
      return false; 
     }); 
     }); 
    })(jQuery); 
</script> 
+0

你使用的是什麼版本的jquery? –

+0

@SMcCrohan jquery.js?ver = 1.11.2:4 – DeDevelopers

+0

請有人幫我.. :( – DeDevelopers

回答

0

此問題正在通過發送由 「CURL」 請求和數據解析.. 的Ajax/JSON是阻斷請求,同時發送查詢.. 因此,我將代碼更改爲CURL請求,它開始工作..

謝謝大家的幫助。

2

假設你的授權頭是正確的,也許你想,而不是嘗試。

的幾點注意事項:
1)變化數據類型到的dataType
2)除去 '內容 - 類型':從標題
3 '應用/ JSON')與來自下面的格式

開始替換數據內容步驟1並測試。如果失敗,則繼續步驟2等

$.ajax({ 
 
    type: "POST", 
 
    url: "https://api.MYEWELLNESS.com/api/v1/services/membership/create", 
 
    headers: { 
 
    'Authorization':'Basic ' + btoa('VitalAlert:5kpN9cYTafnVmZTS')' 
 
    }, 
 
    data: { 
 
     user_name: api_user_name, 
 
     password: api_user_password, 
 
     ... 
 
     ... 
 
    }, 
 
    dataType: "json", 
 
    success: function(msg){ 
 
     console.log("Success: "+msg); 
 
     alert("success"); 
 
     alert(data); 
 
     return flase; 
 
    }, 
 
    error: function(error){ 
 
     console.log("Failed: "+error); 
 
     alert("false"); 
 
     alert(data); 
 

 
    return false; 
 
    } 
 
});

$.ajax({ 
       type: "POST", 
       url: "https://api.MYEWELLNESS.com/api/v1/services/membership/create", 
       headers: { 
        'Authorization':'Basic VitalAlert:5kpN9cYTafnVmZTS' 
        //'Content-Type':'application/json' 
       }, 
       data: { 
     user_name: api_user_name, 
     password: api_user_password, 
     first_name: api_payment_first_name, 
     last_name: api_payment_last_name, 
     address1: api_client_address_one, 
     address2: api_client_address_two, 
     city: api_client_city, 
     state: api_client_state, 
     country: "US", 
     zip: api_client_zip, 
     email: api_user_email, 
      phone: api_client_home_phone, 
      work_phone: api_client_work_phone, 
      cell_phone: api_client_cell_phone, 
      gender: api_client_gender, 
      birthdate: "21-04-1990" 
    }, 
       //data:dpost, 
       dataType: "json",/* 
       beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic " +"Vml0YWxhbGVydA==:MTI5OTky"); },*/ 
       success: function(msg){ 
        console.log("Success: "+msg); 
        alert("success"); 
        alert(data); 
        return flase; 
       }, 
       error: function(error){ 
        console.log("Failed: "+error); 
        alert("false"); 
        alert(data); 

        return false; 
       } 
      }); 
+0

檢查我已經在snipet下面添加了更新後的代碼..讓我知道如果我寫了變量和字符串嗎? – DeDevelopers

+0

@DeDevelopers也許你可以先試試.. 。 – vincentsty

+0

我只是問數據中的內容..字符串將被添加像我一樣添加..只是告訴我的內容... @vincentsty – DeDevelopers