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

JQuery Ajax まとめ

Posted at
//ページ取得(GET)

$(document).on('submit', '.フォーム名', function (event) {

    let form = event.currentTarget;

    //パラメータ全て
    //let queryParam = $(form).serialize();

    //パラメータ指定
    //let queryParam = new URLSearchParams;
    //queryParam.append('パラメータ名', 'パラメータ値');

    const url = `コントローラ名/メソッド名?${queryParam.toString()}`;

    $.ajax({
        url: url,
        type: 'GET',
        timeout: 20000,
    }).done(function (data) {
        $('.追加対象').append(data);
    }).fail(function (error) {
        if (error.status.toString().length <= 4) {
            console.log(`${error.status}:通信エラー`);
        }
        else {
            console.log(`${error.status}:${error.responseText}`);
        }
    }).always(function () {

    });
    return false;
});


//ページ取得(POST)
//FromData

$(document).on('submit', '.フォーム名', function (event) {

    let form = event.currentTarget;

    //全て
    //let requestArray = $(form).serializeArray();
    //var fromData = {};
    //$(requestArray).each(function (index, obj) {
    //    fromData[obj.name] = obj.value;
    //});

    //指定
    //let fromData = {
    //    'キー名': 'バリュー値',
    //    'キー名': 'バリュー値'
    //};

    const url = `home/privacy`;

    $.ajax({
        url: url,
        type: 'POST',
        data: fromData,
        timeout: 20000,
    }).done(function (data) {
        $('.追加対象').append(data);
    }).fail(function (error) {
        if (error.status.toString().length <= 4) {

        }
        else {

        }
    }).always(function () {

    });
    return false;
});




//ページ取得(POST)
//Json

$(document).on('submit', '.フォーム名', function (event) {

    let form = event.currentTarget;

    //全て
    //let requestArray = $(form).serializeArray();
    //var jsonParam = {};
    //$(requestArray).each(function (index, obj) {
    //    jsonParam[obj.name] = obj.value;
    //});

    //指定
    //let jsonParam = {
    //    'キー名': 'バリュー値',
    //    'キー名': 'バリュー値'
    //};

    const url = `home/privacy`;

    $.ajax({
        url: url,
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(jsonParam),
        timeout: 20000,
    }).done(function (data) {
        $('.追加対象').append(data);
    }).fail(function (error) {
        if (error.status.toString().length <= 4) {

        }
        else {

        }
    }).always(function () {

    });
    return false;
});


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