Kirim data json dalam javascript ajax

Fungsi panggilan balik yang dijalankan jika permintaan berhasil. Wajib jika

$.post( "ajax/test.html", function( data ) {

$( ".result" ).html( data );

});

1 disediakan, tetapi dapat

$.post( "ajax/test.html", function( data ) {

$( ".result" ).html( data );

});

2 dalam kasus tersebut

  • tipe data

    Jenis.

    Jenis data yang diharapkan dari server. Bawaan. Tebak Cerdas (xml, json, skrip, teks, html)

  • versi ditambahkan. 1. 12-dan-2. 2

    • pengaturan

      Jenis.

      Satu set pasangan kunci/nilai yang mengonfigurasi permintaan Ajax. Semua properti kecuali

      $.post( "ajax/test.html", function( data ) {

      $( ".result" ).html( data );

      });

      _3 bersifat opsional. Default dapat diatur untuk opsi apa pun dengan $. ajaxSetup(). Lihat daftar lengkap semua pengaturan. Jenis akan secara otomatis diatur ke

      $.post( "ajax/test.html", function( data ) {

      $( ".result" ).html( data );

      });

      4

  • Ini adalah fungsi singkatan Ajax, yang setara dengan

    1

    2

    3

    4

    5

    6

    7

    $.ajax({

    type: "POST",

    url: url,

    data: data,

    success: success,

    dataType: dataType

    });

    _

    Fungsi callback

    $.post( "ajax/test.html", function( data ) {

    $( ".result" ).html( data );

    });

    5 diteruskan dengan data yang dikembalikan, yang akan berupa elemen root XML atau string teks bergantung pada tipe MIME respons. Itu juga melewati status teks dari respons

    Pada jQuery 1. 5, fungsi panggilan balik

    $.post( "ajax/test.html", function( data ) {

    $( ".result" ).html( data );

    });

    5 juga diteruskan a (di jQuery 1. 4, melewati objek

    $.post( "ajax/test.html", function( data ) {

    $( ".result" ).html( data );

    });

    7)

    Sebagian besar implementasi akan menentukan penangan sukses

    1

    2

    3

    $.post( "ajax/test.html", function( data ) {

    $( ".result" ).html( data );

    });

    Contoh ini mengambil cuplikan HTML yang diminta dan menyisipkannya di halaman

    Halaman yang diambil dengan

    $.post( "ajax/test.html", function( data ) {

    $( ".result" ).html( data );

    });

    4 tidak pernah di-cache, sehingga opsi ________0______9 dan

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    0 di

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    1 tidak berpengaruh pada permintaan ini

    Objek jqXHR

    Pada jQuery 1. 5, semua metode Ajax jQuery mengembalikan superset dari objek

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    2. Objek jQuery XHR ini, atau "jqXHR", dikembalikan oleh

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    3 mengimplementasikan antarmuka Janji, memberikannya semua properti, metode, dan perilaku Janji (lihat Objek yang ditangguhkan untuk informasi lebih lanjut).

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    _4 (untuk sukses),

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    5 (untuk kesalahan), dan

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    6 (untuk penyelesaian, apakah berhasil atau salah; ditambahkan di jQuery 1. 6) metode mengambil argumen fungsi yang dipanggil saat permintaan berakhir. Untuk informasi tentang argumen yang diterima fungsi ini, lihat bagian dokumentasi

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    7

    Antarmuka Janji juga memungkinkan metode Ajax jQuery, termasuk

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    8, untuk menghubungkan beberapa

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    9,

    $.post( "test.php" );

    0, dan

    $.post( "test.php" );

    1 panggilan balik pada satu permintaan, dan bahkan untuk menetapkan panggilan balik ini setelah permintaan selesai. Jika permintaan sudah selesai, callback akan segera diaktifkan

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    Pemberitahuan Penghentian

    Metode callback ________21______2,

    $.post( "test.php" );

    3, dan

    $.post( "test.php" );

    4 dihapus pada jQuery 3. 0. Anda dapat menggunakan

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    4,

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    5, dan

    // Assign handlers immediately after making the request,

    // and remember the jqxhr object for this request

    var jqxhr = $.post( "example.php", function() {

    alert( "success" );

    })

    .done(function() {

    alert( "second success" );

    })

    .fail(function() {

    alert( "error" );

    })

    .always(function() {

    alert( "finished" );

    });

    // Perform other work here ...

    // Set another completion function for the request above

    jqxhr.always(function() {

    alert( "second finished" );

    });

    6 sebagai gantinya

    catatan tambahan

    • Karena pembatasan keamanan browser, sebagian besar permintaan "Ajax" tunduk pada kebijakan asal yang sama;
    • Jika permintaan dengan jQuery. post() mengembalikan kode kesalahan, itu akan gagal secara diam-diam kecuali skrip juga memanggil global. metode ajaxError(). Atau, pada jQuery 1. 5, metode

      $.post( "test.php" );

      _8 dari objek

      $.post( "test.php" );

      9 dikembalikan oleh jQuery. post() juga tersedia untuk penanganan kesalahan

    Contoh

    Minta tes. php, tetapi abaikan hasil pengembalian

    1

    $.post( "test.php" );

    Minta tes. php dan kirim beberapa data tambahan bersama (sementara masih mengabaikan hasil yang dikembalikan)

    1

    $.post( "test.php", { name: "John", time: "2pm" } );

    Lewati array data ke server (sementara masih mengabaikan hasil yang dikembalikan)

    1

    $.post( "test.php", { 'choices[]': [ "Jon", "Susan" ] } );

    Kirim data formulir menggunakan permintaan Ajax

    1

    $.post( "test.php", $( "#testform" ).serialize() );

    Peringatkan hasil dari meminta tes. php (HTML atau XML, tergantung pada apa yang dikembalikan)

    1

    2

    3

    $.post( "test.php", function( data ) {

    alert( "Data Loaded: " + data );

    });

    Peringatkan hasil dari meminta tes. php dengan muatan data tambahan (HTML atau XML, tergantung pada apa yang dikembalikan)

    1

    2

    3

    4

    $.post( "test.php", { name: "John", time: "2pm" })

    .done(function( data ) {

    alert( "Data Loaded: " + data );

    });

    Posting ke tes. php dan dapatkan konten yang telah dikembalikan dalam format json ("John", "time" => "2pm"));

    Bagaimana cara mengirim data dalam format JSON di AJAX?

    Bagaimana cara mendapatkan data JSON menggunakan AJAX JavaScript?

    Untuk meminta JSON dari URL menggunakan JavaScript/AJAX, Anda perlu mengirim permintaan HTTP GET ke server dan memberikan Accept. tajuk permintaan aplikasi/json dengan permintaan Anda . Header Terima memberi tahu server bahwa klien JavaScript/AJAX kami mengharapkan JSON.

    Bagaimana cara mengirim data menggunakan AJAX di JavaScript?

    Tetapi ada baiknya juga mengetahui cara mengirim permintaan AJAX dengan Javascript biasa. Gunakan objek XMLHttpRequest untuk berkomunikasi dengan server. .
    Buat Tabel. .
    Konfigurasi Basis Data. .
    Buat tata letak HTML. .
    Buat file PHP untuk menangani permintaan AJAX. .
    Cara membuat permintaan AJAX dengan JavaScript. .
    Keluaran. .
    Kesimpulan

    Bagaimana cara mengirim JavaScript data JSON?

    Kirim Data JSON dari Sisi Klien .
    Buat objek JavaScript menggunakan sintaks standar atau literal
    Gunakan JSON. stringify() untuk mengonversi objek JavaScript menjadi string JSON
    Kirim string JSON yang disandikan URL ke server sebagai bagian dari Permintaan HTTP