<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <link rel="stylesheet" href="css/1.css"/> <link rel="stylesheet" href="css/normalize.css"/> <script src="js/jquery.mockjax.js"></script> <script src="js/jquery-2.2.3.min.js"></script> <script src="js/1.js"></script> </head> <body> name:<input id="name" autofocus placeholder="your name" type="text" > tel:<input id="tel" autofocus placeholder="your telephone number" type="text"> e-mail:<input id="email" autofocus placeholder="your e-mail" type="text"> <button id="btn">submit</button> </body> </html>
window.onload = function(){ $('#btn').click(function () { var name = $('#name').val(); var tel = $('#tel').val(); var email = $('#email').val(); var data = {"name": name, "tel": tel, "email":email}; console.log(data); $.ajax({ type: 'POST', url: 'api/comment/submit', data: JSON.stringify(data), contentType: "application/json", dataType: 'json', success: function (data) { console.log(data); if (data.code != 1) { //console.log(data.msg); alert(data.msg); } else { alert('短信发送成功,我们将尽快联系您'); } } }); $.mockjax({ // 固定 必要 url: 'api/comment/submit', // 必要 提交到后台的URL,要与ajax中的url一致 status: 200, //响应状态码 可选 responseTime: 1000, // 响应时间 可选 responseText: { // 固定 必要 {} 花括号内是后台返回的json数据格式 "code": 1, "msg": 'success' } }) }) };
Fiona2017