[removed] How to tell whether AJAX response is JSON

后端 未结 5 1712
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-29 02:22

I\'ve got an AJAX request that expects JSON in response.

But there\'s a possibility that what gets returns may not be JSON, but rather an HTML error page (unfortunat

5条回答
  •  长发绾君心
    2020-12-29 02:55

    jQuery parseJSON function can be used for this. It will throw an exception then you can catch it go ahead.

    data = '{}';
    try {
        json = $.parseJSON(data);
    } catch (e) {
        // not json
    }
    

提交回复
热议问题