برای استفاده از این بخش باید وارد حساب کاربریت بشی
ورود/ثبت نام
const fname = prompt("لطفاً نام خود را وارد کنید:");
const lname = prompt("لطفاً نام خانوادگی خود را وارد کنید:");
const age = prompt("لطفاً سن خود را وارد کنید:");
// ایجاد یک شی برای ارسال به سرور
const data = {
fname: fname,
lname: lname,
age: age
};
// ارسال درخواست POST با استفاده از Fetch API
fetch('/test.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json' // نوع محتوا را مشخص میکند
},
body: JSON.stringify(data) // دادهها را به فرمت JSON تبدیل میکند
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json(); // انتظار پاسخ JSON
})
.then(data => {
console.log('Success:', data); // نمایش موفقیت در کنسول
})
.catch((error) => {
console.error('Error:', error); // نمایش خطا در کنسول
});
const fname = prompt("Enter first name:");
const lname = prompt("Enter last name:");
const age = prompt("Enter age:");
fetch('/test.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ fname, lname, age })
})
.then(response => response.json())
.then(data => console.log(data))
.catch(error => console.error('Error:', error));
<!DOCTYPE html>
<html lang="fa" dir="rtl">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Practice</title>
<style>
</style>
</head>
<body>
<script>
let fname = prompt("Please Enter Name :")
let lname = prompt("Please Enter Last Name :")
let age = prompt("Please Enter Age :")
let userData = {
fname:fname,
lname:lname,
age:age
}
fetch('/test.php',{method:'POST',
body: JSON.stringify(userData)
})
</script>
</body>
</html>
برای استفاده از این بخش باید وارد حساب کاربریت بشی
ورود/ثبت نام