AJAX: Check if a string is JSON?

前端 未结 7 2050
甜味超标
甜味超标 2020-12-02 21:32

My JavaScript sometimes crashes on this line:

var json = eval(\'(\' + this.responseText + \')\');

Crashes are caused when the argument of <

7条回答
  •  攒了一身酷
    2020-12-02 22:38

    Hers's the jQuery alternative...

    try
    {
      var jsonObject = jQuery.parseJSON(yourJsonString);
    }
    catch(e)
    {
      // handle error 
    }
    

提交回复
热议问题