[英]Passing JSON as parameter to a Javascript method
I am new to Javascript. 我是Java语言的新手。 I am writing a Java code which internally write code to call predefined Javascript method.
我正在编写一个Java代码,该代码在内部编写代码以调用预定义的Javascript方法。
I have a Javascript method defined as: 我有一个Javascript方法定义为:
function myfunction(url, params) {
$.post(url, params);
}
This send params which is a JSON to the URL as post (I suppose). 这会将参数(JSON)作为JSON发送到URL(我想)。
My Java code generate HTML on the fly which internally generate something like: 我的Java代码即时生成HTML,该HTML内部生成类似以下内容的代码:
<iframe onLoad="myfunction(myinternal, {"system":"abcdef", "token": "12345"})"> ......
However, this fails to run. 但是,这无法运行。 Page inspection gave me:
页面检查给了我:
SyntaxError: invalid property id
myfunction(myinternal, {
^
What did I do wrong here? 我在这里做错了什么?
Many thanks 非常感谢
Your HTML has syntax errors. 您的HTML包含语法错误。 Should be more like this:
应该更像这样:
<iframe onLoad="myfunction(myinternal, {\"system\":\"abcdef\", \"token\": \"12345\"})">
or wrap in a single quote: 或换成单引号:
<iframe onLoad='myfunction(myinternal, {"system":"abcdef", "token": "12345"})'>
EDIT: (per first comment) 编辑:(每个第一条评论)
<iframe onLoad="myfunction(myinternal, {"system":"abcdef", "token": "12345"})">
您需要在json对象中转义引号。
You used double quotes "
in your JSON. They are interpreted by the HTML parser as the end of the onLoad
attribute. 您在JSON中使用了双引号
"
。HTML解析器将其解释为onLoad
属性的结尾。
Try with single quotes, like this : 尝试使用单引号,如下所示:
<iframe onLoad="myfunction(myinternal, {'system':'abcdef', 'token': '12345'})">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.