2016-07-28 28 views
0

我有CakePHP 2輸出JSON,並且我已經在Xcode iPhone模擬器中加載了那個JSON數據的Swift 2代碼。CakePHP 2 requestAction打破了我的JSON編碼Swift ...無形中

在我的控制器,這裏產生的JSON代碼:

public function iosDemoIndex() { 
    $studentDataIn = $this->Student->getStudentsByUserId(298, 'rASC'); 
    $studentDataOut = []; 
    foreach ($studentDataIn as $student) { 
     //$currentIep = $this->Iep->requestAction('/ieps/getCurrentIep/' . $student['Student']['id']); 
     $goals = $this->Goal->find('all', array('conditions' => array('Goal.student_id =' => $student['Student']['id']))); 
     $goalHolder = array(); 
     foreach ($goals as $goal) { 
      array_push($goalHolder, $goal['Goal']['goal_title']); 
     } 
     array_push($studentDataOut, array('studentName' => $student['Student']['l_name']. ", " . $student['Student']['f_name'], 'studentGoals' => $goalHolder)); 
    } 
    $this->set(array(
    'students' => $studentDataOut, 
    '_serialize' => array('students') 
    )); 
} 

工作正常。我的Swift代碼通過JSON成功地將學生數組加載到一個結構數組中。但是,如果我只需取消註釋行5(它抓住的數據,我最終希望利用限制在第6行查找),Xcode中給了我:

Error with Json: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 2." UserInfo={NSDebugDescription=Invalid value around character 2.} 

5號線不應該改變任何的JSON文件辦法。而當我看着JSON本身時,無論該行是否註釋掉,它看起來都是一樣的。

所以,任何想法什麼是一個無關的,應該是無形的requestAction可以做的,這是打破我的JSON ...以我看不見的時候我看不到的方式?謝謝。

回答

0

後代(future-me):

這是一個登錄問題。 JSON在我的瀏覽器中看起來相同的原因是當時我在瀏覽器中有一個活動的用戶會話。沒有這個會話(例如,當iOS模擬器嘗試加載JSON頁面時),CakePHP返回一個重定向到登錄。可行的解決方案包括在JSON生成控制器中複製requestAction代碼(儘管這不是非常乾燥),或者在Swift方面建立適當的登錄處理。