Why does $.getJSON silently fail?

前端 未结 5 1092
闹比i
闹比i 2020-12-02 11:16

It seems very inconvenient that jQuery\'s $.getJSON silently fails when the data returned is not valid JSON. Why was this implemented with silent failure? What is the easies

5条回答
  •  孤城傲影
    2020-12-02 11:44

    You can use $.ajax instead, and set the dataType options to "json". From the documentation:

    "json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)

提交回复
热议问题