document.getElementById('messageForm').addEventListener('submit', function(event) { event.preventDefault(); // 阻止表单默认提交行为 const name = document.getElementById('name').value; if (name.trim() === '') { alert('名字不能为空!'); return; } const email = document.getElementById('email').value; if (email.trim() === '') { alert('邮件不能为空!'); return; } const company = document.getElementById('company').value; if (company.trim() === '') { alert('公司名称不能为空!'); return; } const subject = document.getElementById('subject').value; if (subject.trim() === '') { alert('请选择咨询主题!'); return; } const tel = document.getElementById('tel').value; if (tel.trim() === '') { alert('联系电话不能为空!'); return; } const message = document.getElementById('message').value; if (message.trim() === '') { alert('留言不能为空!'); return; } /* fetch('https://mlms.mayfocus.com/app/System/GetSysDictionaryDetailList?DicValue=561000', { method: 'GET', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ message: message }) }) .then(response => response.json()) .then(data => { console.log('成功:', data); displayMessages(data); // 假设返回的数据中包含所有留言信息 }) .catch((error) => { console.error('Error:', error); }); */ /* var xhr = new XMLHttpRequest(); var url = '你的后端接口URL'; xhr.open('POST', url, true); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { // 请求成功,处理返回的数据 console.log(xhr.responseText); } }; var data = JSON.stringify({ key: 'value' }); // 需要发送的数据 xhr.send(data); */ function fetchData(url,meathod, callback) { var xhr = new XMLHttpRequest(); xhr.open(meathod, url, true); xhr.setRequestHeader('Content-Type', 'application/json'); xhr.onreadystatechange = function () { if (xhr.readyState === 4 && xhr.status === 200) { callback(JSON.parse(xhr.responseText)); //console.log(xhr.responseText); } }; xhr.send(data); } var data = JSON.stringify({ name: name, companyName:company, province: 0, city: 0, district: 0, tel: tel, email: email, content: message, themeContent: subject}); // 需要发送的数据 // 使用示例 fetchData('https://web.mayfocus.com/app/NewsContentFrontEnd/AddMessageManage', 'POST', function(CBdata) { console.log(CBdata); if(CBdata.code === 10010) { //layer.msg(name + ', 您已提交成功,请等待回复。'); alert(name + ', 您已提交成功,我们会尽快给于回复。'); document.getElementById('name').value=""; document.getElementById('email').value=""; document.getElementById('company').value=""; document.getElementById('tel').value=""; document.getElementById('subject').value=""; document.getElementById('message').value=""; return; } }); /* fetch('http://114.132.183.208:26300/app/NewsContentFrontEnd/AddMessageManage', { // 向后端发送 POST 请求 method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: name, companyName:company, province: 0, city: 0, district: 0, tel: tel, email: email, content: message, themeContent: subject }) // 将输入转换为 JSON 格式发送到服务器 }) .then( response => response.json()) // 处理响应(尽管这里我们不需要响应体) .then(() => { // 在提交成功后重新加载留言列表(可选) fetchMessages(); // 使用 fetchMessages 函数重新加载留言列表(需定义) }) .catch(error => console.error('Error:', error)); // 处理错误情况(可选) function fetchMessages() { // 获取并显示所有留言的函数(可选) fetch('http://114.132.183.208:26300/app/NewsContentFrontEnd/AddMessageManage') // 从后端获取留言列表的 GET 请求 .then(response => response.json()) // 处理响应体为 JSON 格式的数组数据流对象(需定义) } */ // POST请求示例 /* fetch('https://web.mayfocus.com/app/NewsContentFrontEnd/AddMessageManage', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: name, companyName:company, province: 0, city: 0, district: 0, tel: tel, email: email, content: message, themeContent: subject }) }) .then(response => response.json()) .then(data => console.log(data)); */ } ); ///////////////////////////////////////////////// /* function fetchMessages() { // 获取并显示所有留言的函数(可选) fetch('http://114.132.183.208:26300/app/NewsContentFrontEnd/AddMessageManage') // 从后端获取留言列表的 GET 请求 .then(response => response.json()) // 处理响应体为 JSON 格式的数组数据流对象(需定义) */ /* $.ajax({ url: 'https://mlms.mayfocus.com/app/System/GetSysDictionaryDetailList?DicValue=', type: 'GET', success: function(data) { console.log(data); }, error: function(xhr, status, error) { console.error("Error: " + error); } }); */ function displayMessages(messages) { const messagesDiv = document.getElementById('messages'); messagesDiv.innerHTML = ''; // 清空现有内容 messages.forEach(msg => { const p = document.createElement('p'); p.textContent = msg.message; // 假设每个留言对象有一个message属性 messagesDiv.appendChild(p); }); }