-1

I am having issue with an API while calling it through Jquery/Ajax.. I am getting 405 error... Can anyone please fix my code, so i can make it workable.. please i really need it.

error i am getting:

enter image description here

here is my code:

<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>
DeDevelopers
  • 533
  • 1
  • 6
  • 25

2 Answers2

2

Assuming that your Authorization header is correct, maybe you would like to try this instead.

A few notes:
1) change datatype to dataType
2) remove 'Content-Type':'application/json' from header
3) replace data content with following format

Start from step 1 and test. If fail, continue with step 2 etc.

$.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;
                }
            });
vincentsty
  • 2,127
  • 4
  • 21
  • 39
  • check i have added the updated code below your snipet.. let me know if i wrote the variables and strings right?? – DeDevelopers Oct 23 '15 at 17:58
  • I am just asking about the content in data.. strings will be added like the same i added.. just tell me content ... @vincentsty – DeDevelopers Oct 23 '15 at 18:04
  • headers: { 'Authorization':'Basic ' + btoa('VitalAlert:5kpN9cYTafnVmZTS') }, – vincentsty Oct 24 '15 at 13:14
  • header going goodm but its the cross domain issue i am facing .... now error i am getting is 405 method not found.. which is most probably cross domain issue – DeDevelopers Oct 24 '15 at 14:35
0

This issue is being resolved by sending the request and data from the "CURL".. Ajax/Json is blocking the request while sending query.. So I change the code to CURL request and it's start working..

Thank you everyone for your help.

DeDevelopers
  • 533
  • 1
  • 6
  • 25