在和IOS對接時,我這邊接口返回的json數據默認在數據的尾部添加了個0.如圖:
然後查看了接口處理的方法,並沒有多寫0。
後來在IOS的指點下,發現是OC的請求框架 對 數據格式要求嚴格,需要嚴格的json格式。
解決方式就是在header設置 content-type:text/json
由於使用的是fastadmin,直接在封裝的方法里加入就行
/**
* 返回封裝後的 API 數據到客戶端
* @access protected
* @param mixed $msg 提示信息
* @param mixed $data 要返回的數據
* @param int $code 錯誤碼,默認爲0
* @param string $type 輸出類型,支持json/xml/jsonp
* @param array $header 發送的 Header 信息
* @return void
* @throws HttpResponseException
*/
protected function result($msg, $data = null, $code = 0, $type = null, array $header = ['content-type:text/json'])
{
$result = [
'code' => $code,
'msg' => $msg,
'time' => Request::instance()->server('REQUEST_TIME'),
'data' => $data,
];
// 如果未設置類型則自動判斷
$type = $type ? $type : ($this->request->param(config('var_jsonp_handler')) ? 'jsonp' : $this->responseType);
if (isset($header['statuscode'])) {
$code = $header['statuscode'];
unset($header['statuscode']);
} else {
//未設置狀態碼,根據code值判斷
$code = $code >= 1000 || $code < 200 ? 200 : $code;
}
$response = Response::create($result, $type, $code)->header($header);
throw new HttpResponseException($response);
}
但是header設置原理的代碼還是沒有看懂,有時間思考下