Proper way to catch exception from JSON.parse

后端 未结 5 1967
轻奢々
轻奢々 2020-12-02 07:13

I’m using JSON.parse on a response that sometimes contains a 404 response. In the cases where it returns 404, is there a way to catch an exception and then exec

5条回答
  •  醉话见心
    2020-12-02 08:04

    i post something into an iframe then read back the contents of the iframe with json parse...so sometimes it's not a json string

    Try this:

    if(response) {
        try {
            a = JSON.parse(response);
        } catch(e) {
            alert(e); // error in the above string (in this case, yes)!
        }
    }
    

提交回复
热议问题