2016-12-21 5 views
0

skyscannerAPIに$ http.postを送信できました:http://partners.api.skyscanner.net/apiservices/pricing/v1.0 POST http://partners.api.skyscanner.net/apiservices/pricing/v1.0 405 (Method Not Allowed)を取得しました。 Chromeを使用しているので、拡張子Allow-Control-Allow-Originをインストールしましたが、まだエラーが表示されています。 このようにすることを示す完全なエラーメッセージ:Chromeを使用した角型イオンPOST 405(方法が許可されていません)

POST http://partners.api.skyscanner.net/apiservices/pricing/v1.0 405 (Method Not Allowed) 
    (anonymous) @ ionic.bundle.js:25005 
    sendReq @ ionic.bundle.js:24798 
    serverRequest @ ionic.bundle.js:24508 
    processQueue @ ionic.bundle.js:29132 
    (anonymous) @ ionic.bundle.js:29148 
    $eval @ ionic.bundle.js:30400 
    $digest @ ionic.bundle.js:30216 
    $apply @ ionic.bundle.js:30508 
    (anonymous) @ ionic.bundle.js:65428 
    defaultHandlerWrapper @ ionic.bundle.js:16792 
    eventHandler @ ionic.bundle.js:16780 
    triggerMouseEvent @ ionic.bundle.js:2953 
    tapClick @ ionic.bundle.js:2942 
    tapTouchEnd @ ionic.bundle.js:3069 

(匿名)ionic.bundle.js @:25005は、次のコードされています

xhr.send(isUndefined(post) ? null : post); 

私のコードを以下のようになります。

//service.js  
.service('skyscanner',function($http){ 
     var baseUrl= "http://partners.api.skyscanner.net/apiservices/pricing/v1.0"; 
     var bodyInfo= { 
      body: { 
       apikey: My_API_KEY, 
       Country: "CA", 
       Currency: "CAD", 
       //more data...... 
      } 
      }; 
     this.getKey= function(){ 
     var require_sessionkey= $http({ 
      method:"POST", 
      url:baseUrl, 
      data: bodyInfo, 
      headers: { 
      'Content-Type': 'application/x-www-form-urlencoded', 
      'Accept' :'application/json' 
      } 
     }) 
     .success(function successCallback() { 
      var polling={}; 
      var session_key = require_sessionkey.headers["location"]; 
      (function(){ 
      polling=$http.get(session_key, {query: {apikey: My_API_KEY}}); 
      })(); 
      return polling; 
     }).error(function errorCallback() { 
      console.log("something gets wrong: "+ require_sessionkey); 
     }); 
     }; 
    }) 

//controller.js 
.controller('FlightSearchCtrl',function($scope,skyscanner,FlightInfos){ 
      $scope.list = []; 
      $scope.text = 'hello'; 
      $scope.skyscannerPost= function(){ 
      var polling=skyscanner.getKey(); 
      $scope.polling=polling; 
      }; 
     }) 

答えて

0

2時間の研究の後、私は答えを見つけました。 Content-Type: 'application/x-www-form-urlencoded'はサーバーに送信されるHTTPメッセージの本文が本質的に1つの巨大なクエリ文字列であるため、データをシリアル化する必要があります。名前と値のペアはアンパサンド(&)で区切られ、値は等号(=)で表します。この例は(https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-dataを参照)のようになります。

MyVariableOne=ValueOne&MyVariableTwo=ValueTwo 

データがシリアル化されていない場合は、次のようになります。それはシリアル化された後

console.log(bodyInfo); 
//=> Object {apikey: "ds436195492025844505223424234232173", country: "CA", currency: "CAD", locale: "zh_Hans_CN", adults: 1…} 

:へ

console.log(bodyInfoSerialied); 
    //=>apikey=ds436195492025844505223424234232173&country=CA&currency=CAD&locale=zh_Hans_CN&adults=1&children=0&infants=0&originPlace=YVR&destinationPlace=SHA&outboundDate=2017-01-19&inboundDate=2017-01-23&locationSchema=Iata&cabinClass=Economy 

感謝answer 下記の編集コードを投稿します:

.service('skyscanner',function($http){ 
     var baseUrl= "http://partners.api.skyscanner.net/apiservices/pricing/v1.0"; 
     var bodyInfo= { 
       'apikey': 'ds43619549203213123211202173', 
       "country": "CA", 
       "currency": "CAD", 
       "locale": "zh_Hans_CN", 
       //...  
      }; 
//new added serialize(). 
     serialize = function(obj, prefix) { 
     var str = [], p; 
     for(p in obj) { 
      if (obj.hasOwnProperty(p)) { 
      var k = prefix ? prefix + "[" + p + "]" : p, v = obj[p]; 
      str.push((v !== null && typeof v === "object") ? 
       serialize(v, k) : 
       encodeURIComponent(k) + "=" + encodeURIComponent(v)); 
      } 
     } 
     return str.join("&"); 
     } 
//serialize the body. 
     var bodyInfoSerialied = serialize(bodyInfo); 
     console.log(bodyInfo); 
     console.log(bodyInfoSerialied); 
     this.getKey= function(){ 
     var require_sessionkey= $http({ 
      method:"POST", 
      url:baseUrl, 
      headers: { 
      'Content-Type': 'application/x-www-form-urlencoded', 
      'Accept' :'application/json' 
      }, 
      data:bodyInfoSerialied 
     }) 
     .success(function successCallback(require_sessionkey) { 
      console.log(require_sessionkey); 
      var polling={}; 
      var session_key = require_sessionkey.headers["Location"]; 
      (function(){ 
      polling=$http.get(session_key, {query: {apikey: 'ds43619549203213123211202173'}}); 
      })(); 
      return polling; 
     }).error(function errorCallback(require_sessionkey) { 
      console.log("something gets wrong: "+ require_sessionkey); 
      console.log("bodyInfoSerialied: "+ bodyInfoSerialied); 
     }); 
     }; 
    }) 

コードはまだ間違っていますが、少なくとも405エラーは解決されています。

関連する問題