jQuery Ajax에서 정상적으로 응답이 왔는데 fail 이 호출되는 경우
Your Ajax request contains the following setting:
dataType: "json"
The documentation states that jQuery:
Evaluates the response as JSON and returns a JavaScript object. (...) The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown.
This means that if the server returns invalid JSON with a 200 OK
status then jQuery fires the error function and set the textStatus
parameter to "parsererror"
.
Solution: Make sure that the server returns valid JSON. It is worth noting that an empty response is also considered invalid JSON; you could return {}
or null
for example which validate as JSON.
You can check whether a JSON is valid or not on jsonlint.com.
간단히 번역하자면, dataType을 json으로 해 놓은 경우 반환되는 json의 형태가 올바르지 않을 경우 http response code가 200이 떨어지더라도 fail event가 발생한다. 반환된 json이 정상적인지 확인하려면 jsonlint.com 에 들어가면 가능하다.