How can I prevent the warning 'Property MyProp1 never defined on MyObject'?

后端 未结 5 1974
既然无缘
既然无缘 2021-01-11 10:43

I have some HTML that contains a JSON string. In the on DOM ready callback, I have something like this:

MyObject = JSON.parse($(\'#TheJsonString\').         


        
5条回答
  •  醉酒成梦
    2021-01-11 11:11

    Instead of putting the JSON content in the #TheJsonString object as HTML, you should put it in your page as actual javascript. If the server is generating the content in the page, then there's no reason that the server needs to generate HTML which you then parse. The server can just make a javascript variable inside a script tag and put the actual javascript data structure in it.

    JSON.parse() is very useful for parsing ajax responses, but it really isn't needed when the server can just put the finished javascript right in the generated page in the first place.

提交回复
热议问题