问题通过Ajax传递包含函数的JSON到HTML。

huangapple go评论99阅读模式
英文:

issue passing json2html containing function thru ajax

问题

我正在尝试将XHR请求的responseText分配给一个对象变量,然后通过json2html传递它,以创建一个表单。我遇到的问题是我传递的JSON具有分配给表单项上的onclick事件的函数。这阻止我能够使用JSON.parse()来转换responseText字符串,因为它会在函数调用时出错。我知道如果我能够将它作为对象调用,JSON将起作用,但它不允许我创建该对象;我知道我可以使用ready()函数,但宁愿通过json2html调用函数。我想象中我可能忽略了一些简单的东西,但需要一些帮助来弄清楚如何将字符串转换为可用的格式。以下是一个示例:

//工作正常
var x= {
    "<>":"div",
    "class":"",
    "text":"test",  
    "onclick": function() {
        console.log("click")
    }
};
$('#content').json2html({},x);

//不工作
x=$.ajax({url:u});

x.complete(function(){ 
    //responseText:
    {
        "<>":"div",  
        "class":"btn p-3 bg-white test",
        "text":"test",
        "onclick": function(){console.log("click")}
    }
    //.responseText
    $('#content').json2html({},x["responseText"]);
});
//不工作
英文:

I am trying to assign the responseText of an XHR request to an object variable that I can then pass thru json2html in order to create a form. The problem I am having is the json I'm passing has a function assigned to the onclick event of an item on the form. This keeps me from being able to convert the responseText string using JSON.parse() as it errors out on the function call. I know that the json will work if I'm able to call it as an object but it wont let me create the object; I know that I can use the ready() function but would prefer to pass the function thru the json2html call. I imagine it's something simple I'm overlooking but need some help figuring out how I can get the string into a usable format. As an example:

//works fine
var x= {
    &quot;&lt;&gt;&quot;:&quot;div&quot;,
    &quot;class&quot;:&quot;&quot;,
    &quot;text&quot;:&quot;test&quot;,  
    &quot;onclick&quot;: function(
        {
            console.log(&quot;click&quot;)
        }
    };
    $(&#39;#content&#39;).json2html({},x);

    //does not work
    x=$.ajax({url:u});

    x.complete(function(){ 
    //responseText:
        {
            &quot;&lt;&gt;&quot;:&quot;div&quot;,  
            &quot;class&quot;:&quot;btn p-3 bg-white test&quot;,
            &quot;text&quot;:&quot;test&quot;,
            &quot;onclick&quot;: function(){console.log(&quot;click&quot;)}
        }
//.responseText
        $(&#39;#content&#39;).json2html({},x[&quot;responseText&quot;]);
 });
//.does not work

答案1

得分: 0

首先,你的responseText只是文本,确保在将其发送到json2html之前将文本jsonParse成一个json对象。

其次,你将无法通过ajax传输onclick函数,这是需要在客户端添加的内容。

英文:

Firstly your responseText is just that text, make sure you jsonParse the text first into a json object before you send it to json2html.

Secondly you won't be able to transfer the onclick function via ajax, that's something that will need to be added client side.

huangapple
  • 本文由 发表于 2020年1月3日 15:19:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574619.html
  • json2html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定