LoginSignup
8
7

More than 5 years have passed since last update.

ajaxではまった話(XMLHttpRequest : 500)

Last updated at Posted at 2018-07-12

CakePHP3でajaxで取得した情報を処理しようとしたら、データが取得できなかった。
まずエラーが表示されないので、エラー内容が表示されるように設定する。

.fail(function (XMLHttpRequest, textStatus, errorThrown) {
  console.log("XMLHttpRequest : " + XMLHttpRequest.status);
  console.log("textStatus     : " + textStatus);
  console.log("errorThrown    : " + errorThrown.message);
});

そして、表示されたエラー内容。

XMLHttpRequest : 500
VM1377:70 textStatus     : error
VM1377:71 errorThrown    : undefined

ってことはサーバー側の問題?
とコントローラーの設定を確認したり、データベースを確認したりしたが全く糸口がつかめずお手上げ状態。

しかしエラー修正はあっという間にできた。

$.ajax({
  url: '/ajax',
  type: "post",
  data: {
    // 送信データ
  }
}).done(function (data) {
  console.log(data);
  alert('OK');
}).fail(function (XMLHttpRequest, textStatus, errorThrown) {
  alert("NG");
  console.log("XMLHttpRequest : " + XMLHttpRequest.status);
  console.log("textStatus     : " + textStatus);
  console.log("errorThrown    : " + errorThrown.message);
});
public function ajax()
{
  echo $this->request->is(['ajax']);
  exit;
}

コントローラーで値を返していなかったのが原因でした…。

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