テキストボックスにコードを入力してボタンが押されたら、別サーバのAPIにアクセスして、情報を取得するPGでつまづいています。
failの、「alert("ajaxでExceptionが投げられました");」の方に入ってしまいます。
下記、コードです。
javascript
1function get_intro() { 2 var intro_code = $('#ModelIntroduceCode').val(); 3 $.ajax({ 4 type: 'GET', 5 dataType:'text', 6 url:'Con/getIntroInfo/'+intro_code, 7 }).done(function( 8 var div1 = document.getElementById('name'); 9 div1.innerHTML = '<label id="lbl_name">' + data + '<label/>'; 10 }).fail(function(data){ 11 alert("ajaxでExceptionが投げられました"); 12 }); 13 //サブミット後、ページをリロードしないようにする 14 return false; 15 }
PHP
1public function getIntroInfo($code=0) { 2 $this->layout = ''; 3 // 情報取得APIを実行 4 if (!$this->RequestHandler->isAjax()) { 5 $this->cakeError('error404'); 6 } 7 8 $host = 'https://~.com/api/get_introducer_data.php/?introduce_code=' + $code + '&key=*******'; 9 $opts = array('http' => 10 array( 11 'method' => 'GET', 12 'header' => 'Content-type: application/x-www-form-urlencoded', 13 'ignore_errors' => true 14 ) 15 ); 16 $context = stream_context_create($opts); 17 $json = file_get_contents($host, false, $context); 18 $json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN'); 19 $arr = json_decode($json,true); 20 if ($arr === NULL) { 21 //〜データがない時の処理〜 22 $this->set('nickname', "該当する情報がありません。"); 23 }else{ 24 //〜存在しているときの処理〜 25 // 情報をセッションに保持 26 session_start(); 27 $_SESSION[‘id’] = $arr['id']; 28 // 名前を返却 29 $this->set('name', $arr['name']); 30 } 31 }