Qual é a sua dúvida?
Integração com API externa utilizando HttpSocket e Javascript
Controller CakePHP
... App::uses('HttpSocket', 'Network/Http'); ... public $formHost = null; public $formUser = null; public $formPass = null; /** * beforeFilter * * @return void * @access public */ public function beforeFilter() { parent::beforeFilter(); $this->formHost = Configure::read('Api.forms.host'); $this->formUser = Configure::read('Api.forms.user'); $this->formPass = Configure::read('Api.forms.password'); } ... public function admin_get_byid() { $this->autoRender = false; if ($this->request->is('ajax')) { $query = $this->request->query; try { $loginEP = "{$this->formHost}/api/login"; $endpoint = "{$this->formHost}/api/users/{$query['uId']}"; $loginHeader = $this->loginJwtApi($loginEP, $this->formUser, $this->formPass); $http = new HttpSocket(); $data = $http->get($endpoint, $loginHeader); return $data['body']; } catch (Exception $ex) { return false; } return json_encode($query); } }
View / Javascript
< textarea id="userdata" >< /textarea > ...text/javascript... function getUser(uId) { // Padrão de URL CakePHP let _url = ' false, 'admin' => true, 'controller' => 'forms', 'action' => 'admin_get_byid', ), false); ?>'; $.ajax({ url: _url, cache: false, data: { 'uId': uId }, success: function(data) { console.log(data); let json = JSON.parse(data); json.forEach(function(jsonElem, i) { $("#userdata").val(json[i]); } }, error: function(err) { console.log('error'); } }); }
Tags: Loop com Json via javascript, json loop javascript,