![]() |
Controller can't return json data - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: Controller can't return json data (/showthread.php?tid=77739) |
Controller can't return json data - shoXyRafael - 10-13-2020 Hello, I'm currently working with the PHP framework Codeigniter 4.0.4 and trying to add the PayPal Checkout Intergration with Smart Payment Buttons. Console error: Code: Uncaught SyntaxError: Unexpected token < in JSON at position 0 Serverside: PHP Code: $clientId = getenv('paypal.CLIENT_ID'); Clientside: PHP Code: <script type="text/javascript"> $response outputs the whole page I guess, how I can only return the $response json? RE: Controller can't return json data - T.O.M. - 10-14-2020 Try to use this (according to documentation): PHP Code: return $this->response->setJSON($data); // $data - is array RE: Controller can't return json data - waqaschughtai91 - 04-30-2021 i wanted to implement https://demo.paypal.com/au/demo/download?capability=PayPalCheckoutServerSideV2&lang=PHP its is simple PHP solution(it working fine standalone) i've load constants in my constants file and past the index.php code in my view file and also make a new controller. when i make the Error when i click the javascript paypal button Uncaught SyntaxError: Unexpected token < in JSON at position 0 this error come from this code return fetch( '<?=URL['services']['orderCreate']?>', { method: 'POST', body: formData } ).then(function(response) { return response.json(); ## PRBLEM with this line JSON return issue // console.warn(xhr.response.json()) }).then(function(resJson) { console.log('Order ID: '+ resJson.data.id); return resJson.data.id; }); }, i've mention above there is an issue in return response.json(); above plase tell how to get he response in json format. in CI. i'm using CI 3.x. RE: Controller can't return json data - InsiteFX - 04-30-2021 maybe you should upgrade to the newest version of CodeIgniter 4.1.1 which has bug fixes. Also try sending setting a json header to tell it what your sending. |