1: <?php
2:
3: /**
4: * This file is part of the Nette Framework (https://nette.org)
5: * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
6: */
7:
8: namespace Nette\Application\Responses;
9:
10: use Nette;
11:
12:
13: /**
14: * JSON response used mainly for AJAX requests.
15: */
16: class JsonResponse implements Nette\Application\IResponse
17: {
18: use Nette\SmartObject;
19:
20: /** @var mixed */
21: private $payload;
22:
23: /** @var string */
24: private $contentType;
25:
26:
27: /**
28: * @param mixed payload
29: * @param string MIME content type
30: */
31: public function __construct($payload, $contentType = null)
32: {
33: $this->payload = $payload;
34: $this->contentType = $contentType ? $contentType : 'application/json';
35: }
36:
37:
38: /**
39: * @return mixed
40: */
41: public function getPayload()
42: {
43: return $this->payload;
44: }
45:
46:
47: /**
48: * Returns the MIME content type of a downloaded file.
49: * @return string
50: */
51: public function getContentType()
52: {
53: return $this->contentType;
54: }
55:
56:
57: /**
58: * Sends response to output.
59: * @return void
60: */
61: public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
62: {
63: $httpResponse->setContentType($this->contentType, 'utf-8');
64: echo Nette\Utils\Json::encode($this->payload);
65: }
66: }
67: