JQuery.Ajax()无法运行,脚本配置及语法错误排查求助
问题排查与代码修复
Hey there, let's break down the issues in your code that are causing that Uncaught SyntaxError: Invalid or unexpected token error and fix them up step by step!
主要语法错误点
- 中文引号误用:你的代码里使用了中文双引号
“”(比如type: “POST”、url: “https://mandrillapp.com/api/1.0/messages/send.json”),JavaScript仅识别英文双引号"或单引号',这是触发语法错误的核心原因。 - 括号缺失:
$(document.ready写法错误,正确的DOM就绪事件绑定应该是$(document).ready(function() {}),你漏掉了document后面的闭合括号。
额外功能优化与安全提醒
- 无效链接修复:你在
html字段里的<a>标签缺少href属性,这样的链接无法跳转,需要补充为<a href="你的调查链接地址">link</a>。 - 严重安全风险:绝对不要在前端代码里硬编码API密钥!Mandrill的API密钥拥有高权限,一旦暴露,任何人都能盗用你的账户发送邮件或消耗配额。正确的做法是通过后端服务调用API,前端仅向后端发送请求数据。
修正后的完整代码
$(document).ready(function () { $('#survey').click(function() { $.ajax({ type: "POST", url: "https://mandrillapp.com/api/1.0/messages/send.json", data: { 'key': '你的API密钥(请勿在前端暴露!)', 'message': { 'from_email': '发件人邮箱地址', 'to': [ { 'email': '收件人邮箱地址', 'name': 'RECIPIENT NAME (OPTIONAL)', 'type': 'to' } ], 'autotext': 'true', 'subject': 'Class Survey', 'html': 'Here is your survey link: <a href="https://你的调查链接地址">link</a>' } } }).done(function(response) { console.log(response); }); }); });
内容的提问来源于stack exchange,提问作者Anon211
相关产品推荐
相关产品推荐

