【问题标题】:Api 405 method not allowed jqueryApi 405 方法不允许 jquery
【发布时间】:2016-01-23 07:40:05
【问题描述】:

我在通过 Jquery/Ajax 调用 API 时遇到问题。 我收到 405 错误... 任何人都可以请修复我的代码,所以我可以让它工作..拜托我真的需要它。

我得到的错误:

这是我的代码:

<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>

【问题讨论】:

  • 你用的是什么版本的jquery?
  • @SMcCrohan jquery.js?ver=1.11.2:4
  • 请有人帮帮我..:(
  • 您的代码显示为type: "POST",但您的请求显示为Request Method: OPTIONS。我会开始看那里。
  • 我怎么能解决这个@SMcCrohan

标签: javascript php ajax api http-status-code-405


【解决方案1】:

正在通过从“CURL”发送请求和数据来解决此问题。 Ajax/Json 在发送查询时阻止请求.. 所以我将代码更改为 CURL 请求,它开始工作了..

感谢大家的帮助。

【讨论】:

    【解决方案2】:

    假设您的 Authorization 标头是正确的,也许您想试试这个。

    一些注意事项:
    1) 将数据类型更改为数据类型
    2) 从标题中删除 'Content-Type':'application/json'
    3) 用以下格式替换数据内容

    从第 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;
                    }
                });
    

    【讨论】:

    • 检查我是否已在您的片段下方添加了更新的代码。如果我写的变量和字符串正确,请告诉我??
    • 我只是询问数据中的内容.. 字符串将像我添加的一样添加.. 告诉我内容... @vincentsty
    • 标头:{ 'Authorization':'Basic' + btoa('VitalAlert:5kpN9cYTafnVmZTS') },
    • 标题很好,但它是我面临的跨域问题....现在我得到的错误是 405 方法未找到..这很可能是跨域问题
    猜你喜欢
    • 2013-03-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-04
    • 2021-02-25
    • 2016-03-23
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多