[英]Slim response-headers returning text/html instead of application/json
我對Slim並不陌生,到目前為止沒有遇到任何麻煩-一切都按預期進行,直到出現此問題為止。 我已經搜索了,但是我一定不能在正確的地方尋找。
我在Slim和NotORM中使用AngularJS。 到目前為止,我已經進行了用戶身份驗證,並且正在處理一個簡單的狀態更新表單,該表單將保存到數據庫表-“ post”中。 表單本身很簡單,僅包含ng-model設置為'message.text'的textarea元素。 提交后,在控制器中調用doPost(message):
$scope.doPost = function(message) {
Data.post('message', {
message: message
}).then(function(results) {
Data.toast(results);
loadRemoteData();
}, function(error) {
console.log('message failed to send: ' + error);
});
$scope.message = {
content: ''
}
}
我在數據服務(Data.post('message'))中的代碼是:
var obj = {};
obj.post = function (q, object) {
return $http.post(serviceBase + q, object)
.then(function(results) {
return results.data;
},
function(error) {
console.log('failed -->' + results.data + '<--');
});
};
return obj;
然后是PHP:
$app->post('/message', function() use ($app, $db) {
$response = array();
$r = json_decode($app->request->getBody());
$userid = $_SESSION['uid'];
$displayname = $_SESSION['displayname'];
verifyRequiredParams(array('text'), $r->message);
$message = $db->post();
$data = array(
'userid' => $uid,
'displayname' => $displayname,
'text' => $r->message->text
);
$result = $message->insert($data);
if($result != NULL) {
$response['status'] = 'success';
$response['message'] = 'Post successful';
$response['id'] = $result['id'];
echoResponse(200, $response);
} else {
$response["status"] = "error";
$response["message"] = "Failed to create message. Please try again";
echoResponse(200, $response);
}
});
在echoResponse()中:
function echoResponse($status_code, $response) {
$app = \Slim\Slim::getInstance();
// Http response code
$app->status($status_code);
// setting response content type to json
$app->contentType('application/json');
echo json_encode($response);
}
這幾乎就是代碼。 沒有錯誤,但是message.text沒有發布到數據庫中,返回的響應為空。 我在頁面上創建了另一個表單,其中包含文本類型的輸入字段,並且使用重復方法可以正常工作。 我已經嘗試了所有我能想到的一切,而對我而言突出的是Response-Header的Content-Type以某種方式是text / html而不是application / json(測試表單顯示json)。 我嘗試發布的表如下所示:
CREATE TABLE IF NOT EXISTS `post` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`userid` int(11) NOT NULL,
`displayname` varchar(50) CHARACTER SET utf8,
`text` text CHARACTER SET utf8,
`date` timestamp DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=9 ;
任何幫助,將不勝感激。 謝謝。
這是標題:
Response Headers
Access-Control-Allow-Head... origin, x-requested-with, content-type
Access-Control-Allow-Meth... PUT, GET, POST, DELETE, OPTIONS
Access-Control-Allow-Orig... *
Connection Keep-Alive
Content-Length 0
Content-Type text/html
Date Fri, 09 Jan 2015 11:52:12 GMT
Keep-Alive timeout=5, max=92
Server Apache/2.2.26 (Unix) DAV/2 PHP/5.4.30 mod_ssl/2.2.26 OpenSSL/0.9.8za
X-Powered-By PHP/5.4.30
Request Headers
Accept application/json, text/plain, */*
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Length 32
Content-Type application/json;charset=utf-8
Cookie PHPSESSID=g9ooedu5kk513pk5f5djug42c4
Host localhost
Referer localhost
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:34.0) Gecko/20100101 Firefox/34.0
您可以使用以下方法設置Slim應用程序的全局內容:
$app->contentType('application/json');
您需要獲取$ response,因為它是受保護的對象...
$response = $app->response();
$response->header('Content-Type', 'application/json');
$app->render(
file_get_contents($app->config('templates') . $template), $myPageVars
);
我選擇在模板中實際計算JSON,而不是直接轉儲PHP數據。 允許以后更改數據饋送,而無需破壞數據源。
沒有可接受的答案,因此對於文檔中的記錄(Slim 3):
$newResponse = $oldResponse->withHeader('Content-type', 'application/json');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.