/** * Send JSONP response to output * @param IRequest $httpRequest * @param IResponse $httpResponse * @throws \Drahak\Restful\InvalidArgumentException */ public function send(IRequest $httpRequest, IResponse $httpResponse) { $httpResponse->setContentType($this->contentType ? $this->contentType : 'application/javascript', 'UTF-8'); $data = array(); $data['response'] = $this->data; $data['status'] = $httpResponse->getCode(); $data['headers'] = $httpResponse->getHeaders(); $callback = $httpRequest->getQuery('jsonp') ? Strings::webalize($httpRequest->getQuery('jsonp'), NULL, FALSE) : ''; echo $callback . '(' . $this->mapper->stringify($data, $this->isPrettyPrint()) . ');'; }
/** * Creates and returns response from given payload formatted for selected * output content type. * * @return Nette\Application\IResponse */ protected function createResponse($payload = NULL) { if ($this->outputContentType == 'application/json') { // Don't use JsonResponse because it manages the caching headers return new Nette\Application\Responses\TextResponse(func_num_args() ? Nette\Utils\Json::encode($payload) : NULL); } else { $text = func_num_args() ? (string) Nette\Utils\Html::el('pre', Nette\Utils\Json::encode($payload, Nette\Utils\Json::PRETTY)) : '<span style="font-style: italic;">No data</span>'; $text = '<h1>HTTP ' . $this->httpResponse->getCode() . '</h1>' . $text; return new Nette\Application\Responses\TextResponse($text); } }