1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

PHPで返却しているJSONが途中で切れるバグを解消した

Posted at

環境

PHP 7系
JavaScript (Pure)
Docker
HAProxy 2.2
VSCode Remote Container

小さい Web システムプロジェクトなので、APIを作成するためのライブラリ等は使っていない。

発生した問題

echo json_encode($data); という簡単なコードで、APIを作成していた。

小さいプロジェクトなので、APIを作成するためのライブラリ等は使っていない。

比較的短いJSONを出力すると、APIから返却されたJSONが途中で切れてしまうバグに遭遇した。

{"locale": "ja-JP"}

これぐらいだと {"locale": のように切れてしまう。これが

{"locale": "ja-JP", "dummy": "なんでなんだろうなああああああああああああ"}

のように長いJSONを返すと、切れないで表示される。(JavaScriptが正常にパースできる。)

原因

header('Content-Type: application/json');
echo('{"locale": "ja-JP"}');

このように、echoする前にヘッダを設定する必要があった。ちなみに実際には、JavaScriptのコールバックが必要なJSONPコールだったので、 application/javascript にした。

コメント

あまり使ったことのないHAProxyのせいだと思い込み、かつ似たようなIssueが過去に報告されていたのでかなりはまりました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?