【问题标题】:Eval response.d in Jquery Ajax在 Jquery Ajax 中评估 response.d
【发布时间】:2010-05-14 21:53:16
【问题描述】:

这样做有什么意义:

var resultsArray = (typeof response.d) == 'string' 
    ? eval('(' + response.d + ')') : response.d;

$.(ajax) 调用的 onSuccess() 回调内部?

【问题讨论】:

    标签: jquery ajax


    【解决方案1】:
    var resultsArray =                  /* assign a value to resultsArray...        */
      (typeof response.d) == 'string' ? /* ...if the type of response.d is string   */
      eval('(' + response.d + ')')    : /* ...then evaluate it as if it was JS code */
      response.d;                       /* ...else just assign it unaltered         */
    

    【讨论】:

    • 我的问题实际上是这里的 eval 有什么意义? Web服务在什么特定情况下应该返回可执行代码???
    • @gnomixa:有很多可能的原因,例如,返回可评估的 JavaScript 是 JSON Web 服务的常见做法。它完全取决于实现,因为我不知道 response.d 在您的上下文中实际上是什么,所以我很难给出解释。
    • 我使用它与 asp.net 服务进行通信,该服务总是返回:1)字符串或 2)字符串数组(JSON 格式)。
    • @gnomixa 所以我想“else”部分永远不会真正执行,因为response.d 始终是字符串类型。 (当然,除非有一些不寻常的情况。)
    • ^实际上反过来。我只在返回字符串数组时使用此语句。我正在集中对 ajax 方法的所有调用,因此我发现 eval 对我完全没用。
    猜你喜欢
    • 2011-01-10
    • 1970-01-01
    • 2019-01-23
    • 1970-01-01
    • 2016-08-19
    • 1970-01-01
    • 1970-01-01
    • 2011-01-11
    • 1970-01-01
    相关资源
    最近更新 更多