LoginSignup
3
2

More than 3 years have passed since last update.

JQueryのAjaxの例文

Last updated at Posted at 2019-01-12

jQuery.ajax()

JQuery3でAjaxを使う際にたまに見るけど毎回使いやすい例文が無いのでメモ

$.ajax({
        url: url,
        type: 'POST',
        data: {
            param: $('#param').val()
        },
        headers: {
            'X-Custom-Header': '独自のヘッダ'
        },
        dataType: 'json',
        timeout: 5000
    })
    .done(function(response, textStatus, jqXHR) {
        // success
    })
    .fail(function(jqXHR, textStatus, errorThrown) {
        // error
        var response = JSON.parse(jqXHR.responseText);
        if (jqXHR.status === 404) {
            // 404
        }
        if (jqXHR.status === 0) {
            // timeout
        }
    })
    .always(function() {
        // always
    });
contentType: 'application/json',

を設定するとpayloadになるのでPHP$_POSTだと取れなくなります。

を使って動作確認すると良いかも

3
2
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
3
2