JavaScript တွင် AJAX အသုံးပြု၍ JSON ကို server သို့ပို့ခြင်း
Server သို့ဒေတာများကို JSON ပုံစံဖြင့်ပို့နိုင်ပါသည်။ ၎င်းကိုအောက်ပါအတိုင်းပြုလုပ်ပါသည်:
button.addEventListener('click', function() {
let promise = fetch('/handler/', {
method: 'post',
body: JSON.stringify([1, 2, 3, 4, 5]),
headers: {
'Content-Type': 'application/json',
},
});
});
Server တွင်တောင်းဆိုချက်၏ body သည်
body အတွင်း၌ရှိလိမ့်မည်:
export default {
'/handler/': function({ body }) {
console.log(body);
}
}
Server မှလက်ခံရရှိသော JSON ကို ပြန်ဖွင့်ကြည့်ပါ:
export default {
'/handler/': function({ body }) {
console.log(JSON.parse(body));
}
}
ဒေတာများပါသော array တစ်ခုကို server သို့ပို့ပါ။ ထို array ၏ element များ၏ပေါင်းလဒ်ကို server မှရှာပြီး ပြန်ပို့စေပါ။