LoginSignup
40
40

More than 5 years have passed since last update.

Ajaxでpost送信

Posted at

内容

id="ajax-button"のボタンを押すと、
指定のURLに3秒毎に"param1"と"param2"を送信する。
成功したら"ok"のアラートを、失敗したら"error"のアラートを出す内容です。

$( function() {
    $('#ajax-button').click(
    function() {
        var hostUrl= 'http://localhost:8000';
        var param1 = 1;
        var param2 = 10;
        $.ajax({
            url: hostUrl,
            type:'POST',
            dataType: 'json',
            data : {parameter1 : param1, parameter2 : param2 },
            timeout:3000,
        }).done(function(data) {
                          alert("ok");
        }).fail(function(XMLHttpRequest, textStatus, errorThrown) {
                         alert("error");
        })
    });
} );

これでいらない内容を消したり、paramに変数を入れたりすればOK!

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