我有以下内容:
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;
}
答案 0 :(得分:0)
您的报价缺失:
var params = httpGet("http://localhost/data);
答案 1 :(得分:0)
我使用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))
}