未捕获的语法错误:将字符串传递给函数时出现意外的标记ILLEGAL
Uncaught SyntaxError: Unexpected token ILLEGAL when passing string to function
我有以下内容:
var params = httpGet("http://localhost/data");
console.log(params)
它在控制台中显示:
{
"Success": true,
"Data": {
"requestBody": {
"Text": "{'r'n '"ID'": 1,'r'n '"Name'": '"sample string 2'",'r'n '"Desc'": '"sample string 3'",'r'n '"quibble'": '"sample string 4'",'r'n '"cobble'": 5,'r'n '"abc'": 6,'r'n '"Status'": '"Enabled'",'r'n '"id'": 7'r'n}"
},
"responseBody": {
"Text": "{'r'n '"ID'": 1,'r'n '"Name'": '"sample string 2'",'r'n '"Desc'": '"sample string 3'",'r'n '"quibble'": '"sample string 4'",'r'n '"cobble'": 5,'r'n '"abc'": 6,'r'n '"Status'": '"Enabled'",'r'n '"id'": 7'r'n}"
},
"apiDescr": "description"
},
"ErrorCode": 0,
"ErrorDescription": "",
"Meta": ""
}
然而,当我试图将params
传递给如下函数时:
var params = httpGet("http://localhost/data");
console.log(params)
$(".modal-body").append("<p><a href='"#'" role='"button'" class='"btn btn-default'" onClick='"display_params('" + params + "')'">" + key + "</a>" + " " + d.verbDocDict[key]["document"] + "</p>")
$(".modal-body").append("<div id='"" + key + "'"></div>")
function display_params(params) {
console.log(params)
}
我得到以下错误:
Uncaught SyntaxError: Unexpected token ILLEGAL
编辑
投票结束的人能解释一下这是怎么偏离主题的吗?这样我就可以补救了?
httpGet:
function httpGet(theUrl) {
var xmlHttp = null;
xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false);
xmlHttp.send(null);
return xmlHttp.responseText;
}
您缺少引号:
var params = httpGet("http://localhost/data);
我使用escape
和unescape
函数解决了这个问题:
var params = httpGet("http://localhost/data");
$(".modal-body").append("<p><a href='"#'" role='"button'" class='"btn btn-default'" onClick='"display_params('" + escape(params) + "')'">" + key + "</a>" + " " + d.verbDocDict[key]["document"] + "</p>")
$(".modal-body").append("<div id='"" + key + "'"></div>")
function display_params(params) {
console.log(unescape(params))
}
相关文章:
- 创建一个类似链接的按钮,并通过Javascript函数打开一个新的弹出窗口
- 将函数的上下文应用于javascript变量
- 如何在JavaScript中将字符串转换为函数引用
- 用嵌套函数和默认函数定义函数
- 使用 jQuery 的 .on 函数如何获取事件的原始元素
- 无法导出函数expressjs/requestjs中的变量
- 函数参数中的数据与指定变量之间的任何性能差异
- JQuery合并了keyup和focusout两个函数
- ES6构造函数返回基类的实例
- 监视函数从服务返回不起作用,但作用域函数起作用
- 我可以在json对象中添加一个函数吗
- AngularJS:我可以跳过函数参数回调吗
- 如何使jQuery插件函数可调用以供独立使用,而不在集合上操作
- JavaScript数组排序(函数)用于对表行进行排序,而不是排序
- jquery点击函数select&取消选择
- 拨打'父亲'函数形式a'儿童'ReactJS中的组件
- Node.js v6.2.0类扩展不是函数错误
- PHP调用脚本函数,返回值为Uncaught SyntaxError:意外的令牌ILLEGAL
- 未捕获的语法错误:将字符串传递给函数时出现意外的标记ILLEGAL
- JS Regexp替换"illegal"字符来创建PHP函数名