2017-09-18 2 views
0

iron-ajaxを使用しようとしていますがcontent-type = 'application/json'はサポートしていません。iron-ajaxはPOSTリクエストのcontent-type = 'application/json'をサポートしていません

<iron-ajax id="ajaxRequestOtp" 
           method="POST" 
           url="[[apiEndPoint]]/user-sms-auth" 
           body="{{requestOtpBody}}" 
           handle-as="json" 
           headers='{"Accept": "application/json"}' 
           on-response="_responseRequestOtp" 
           on-error="_errorResponseRequestOtp" 
           content-type='application/json'> 
        </iron-ajax> 

プロパティ -

static get properties() { 
      return { 
       apiEndPoint: { 
        type: String, 
        value: 'http://example.com' 
       }, 
       requestOtpBody: { 
        type: String, 
        computed: '_createRequestOtpBody(mobileNumber)', 
        notify: true 
       } 
      }; 
     } 

計算さ機能 -

_createRequestOtpBody(mobileNumber) { 
      let body = { 
       phone_number: "+91" + mobileNumber 
      } 
      return JSON.stringify(body); 
     } 

これは、404不正な要求を機能していません。 JSONオブジェクトをサーバーに送信する必要があります。

エラーMessage-

OPTIONS http://example.com/user-sms-auth 404 (Not Found) 
(index):1 Failed to load http://example.com/user-sms-auth: Response for preflight has invalid HTTP status code 404 
+0

'鉄ajax'はPOST'' 'のためにそのコンテンツをサポートtype'が、実際にヘッダを設定してJSONデータを送信する:[デモ](https://codepen.io/tony19/pen/rGxMMP ?editors = 1010)。 400の詳細はありますか?要求の問題を示すエラーメッセージはありますか? (例えば、ヘッダーが欠落していたり​​、不正な本文など) – tony19

+0

@ tony19 - 私は** url **を_codepen_ '// httpbin.org/post'で提供されたurlに置き換え、物事は同じで、それは働き始めた。だから問題は私のバックエンドサーバーにあるのですか? – rahul

+0

受け取ったエラーコードを「400 Bad Request」から「404 Bad Request」に変更したようですが、これは正しくありません。** 404 **は「Not Found」のステータスコードです。 ** 400 **を取得している場合は、リクエストに間違いがあることを意味します。 ** 404 **は、存在しないリソース(URLが不正)をリクエストしていることを示します。 – tony19

答えて

0

requestOtpBody =>type: Objectを行い、JSON.stringifyステップをスキップします。

_createRequestOtpBody(mobileNumber) { 
    return { phone_number: "+91" + mobileNumber }; 
} 
+0

私もこれを試しましたが、動作していません – rahul

+0

@rahulは 'with-credentials 'を' iron-ajax'要素に追加しようとしましたか? – codeMonkey

関連する問題