@permission_classes((AllowAny, ))
class Viewset(viewsets.ViewSet):
renderer_classes = (JSONRenderer, )
def list(self, request, *args, **kwargs):
return Response()
null
๋๋ {}
์ ๊ฐ์ ์ ํจํ json ์๋ต ๋ณธ๋ฌธ
Content-Type
ํค๋ ์ธํธ๊ฐ ์๊ณ ๋น ์๋ต์ด ๋ฐํ๋ฉ๋๋ค. ํนํ jquery์ ajax()
์์ฒญํ ๋ ๊ณ ํต์ค๋ฝ์ต๋๋ค. ๊ตฌ๋ฌธ ๋ถ์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํค๊ณ ๋น ๋ณธ๋ฌธ์ด ์๋ ์ฑ๊ณต ์๋ต์ ๋ํ ์ฝ์์ ๊ฑฐ๋ถํฉ๋๋ค. ์ฌ๊ธฐ๋ฅผ ์ฐธ์กฐํ์ญ์์ค https://stackoverflow.com/a/6186905
์ฌ๊ธฐ ํ๋ณด: https://github.com/encode/django-rest-framework/pull/6027
๊ฐ๋จํ ์ค๋ช
์
๋๋ค. ์ ํํ ์ค์ ์ ๋ค์ ์์ฑํ์ง ์์ ์ ์์ง๋ง ๋น ๋ฅธ ์๋ ํ
์คํธ๋ Content-Type
ํค๋๊ฐ application/json
์ฌ๋ฐ๋ฅด๊ฒ ์ค์ ๋์์์ ๋ณด์ฌ์ค๋๋ค.
๋น ์๋ต์ ๊ดํด์๋ @kltdwrds์ ๋์ํ๋ ๊ฒฝํฅ์ด ์์ต๋๋ค. ์ฌ์์ด ๋น ๋ณธ๋ฌธ์ ํ์ฉํ๋์ง ๋ชจ๋ฅด๊ฒ ์ง๋ง JavaScript์ JSON.parse
์ Python์ json.loads
๋น ๋ฌธ์์ด์ ๋์ฝ๋ฉํ์ง ๋ชปํฉ๋๋ค.
์ฆ, data=None
๋ null
๋ก ์ธ์ฝ๋ฉ๋์ด์ผ ํฉ๋๋ค.
๋ค์ ์๊ฐํด๋ณด๋ฉด ํ์ฌ์ ํ๋์ด ์ดํด๊ฐ ๋ฉ๋๋ค. JSON.parse
๋ฐ json.loads
๋ ์คํจํ์ง๋ง ๊ตฌ๋ฌธ ๋ถ์ํ๊ธฐ ์ ์ ์ฝํ
์ธ ์ ํ์ ํ์ธํ๋ ๊ฒ์ ํด๋ผ์ด์ธํธ์ ์ฑ
์์
๋๋ค. ๋ง์ฝ ์๋ค๋ฉด ์ด๊ฒ์ jQuery์ ๋ฒ๊ทธ์
๋๋ค.
ํ ๋ด๊ฐ ์ฌ์ฉํ๋
from rest_framework import viewsets
from rest_framework.renderers import JSONRenderer
from rest_framework.response import Response
Content-Type
ํค๋๊ฐ ์ด๋์์ ์ค์ ๋๋์ง ํ์คํ์ง ์์ง๋ง ์์ ์๋ฅผ ์ฌ์ฉํ์ฌ ์๋ต์์ ๋๋ฝ๋์์์ ํ์ธํ์ต๋๋ค.
Content-Type
ํค๋๊ฐ ๋น ๋ ๋๋ฌ ์๋ต์์ ์ญ์ ๋ ๊ฒ ๊ฐ์ต๋๋ค(๋ผ์ธ 80).
https://github.com/encode/django-rest-framework/blob/master/rest_framework/response.py#L72
return Response()
๋ํ ํ์ฌ ๋์์ ์๋์ ์
๋๋ค. null
๋ฅผ ๋ฐํํ๋ ๊ฒ๋ ์ ํจํ์ง๋ง ๋ด์ฉ ์ ํ์ด ์๋ ๋น ์๋ต์ ๋ฐํํ๋ ๋ฐฉ๋ฒ์ด ํ์ํ ๊ฒ์ด ๋ ์ผ๋ฐ์ ์
๋๋ค. ์ง๊ธ์ ์๋ ๊ทธ๋๋ก ๋๋ ๊ฒ์ด ์ข์ ๊ฒ ๊ฐ์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
Content-Type
ํค๋๊ฐ ๋น ๋ ๋๋ฌ ์๋ต์์ ์ญ์ ๋ ๊ฒ ๊ฐ์ต๋๋ค(๋ผ์ธ 80).https://github.com/encode/django-rest-framework/blob/master/rest_framework/response.py#L72