LoginSignup
2
1

More than 5 years have passed since last update.

AzureFunctionsでHTMLやJSONを返す。HTTPヘッダに手を加える。

Posted at

AzureFunctionsの出力にHTTPを選んだとき、resメソッドにコンテンツを直接代入する形になりますが、これにHTMLやJSONを突っ込む時に上手く解釈してくれなかったので、それについての解決策をメモ。

解決

headersオブジェクト内でContent-Typeを明示的に指定する必要がある。

context.res = {
    status: 200,
    headers: { 'Content-Type': 'application/json' },
    body : { message : 'Hello World!'}
};

結果

スクリーンショット (3).png
ブラウザ側でもちゃんとJSONだと認識してくれます。

症状

context.res = {
    status: 200,
    body : { message : 'Hello World!'}
    // Headerを指定していない
};

結果

スクリーンショット (4).png
クライアントによって解釈の方法がまちまちになる。

終わりに

ヘッダの指定ができるのでクロスオリジン要求にも対応が出来ますね。
JavaScriptを使っていますが、他の言語でも同じように対応できると思います。

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1