Passing javascript code in JSON

后端 未结 2 1315
情话喂你
情话喂你 2021-01-21 05:31

I am making an AJAX call with jQuery to a PHP script that needs to return javascript code embedded in a JSON result for the jQuery code to execute. How do I go about passing jav

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-21 05:56

    The definition of JSON doesn't allow for javascript functions to be transported. It is meant to be language independent. If you use a JSON parser rather than eval(), it will fail if the JSON contains any functions.

    Douglas Crockford, who I believe coined the term JSON has a JSON parser for javascript

    Even JSONP doesn't return a function but a function call - myFuncIAskedFor({ returned JSON })
    As others have mentioned, define your functions locally, it's the safest thing to do
    Also use a JSON parser

提交回复
热议问题