JSON形式で返ってきてるはずなのに。
うけとったJavaScript側でふたたびJSON.parse
するハメに。
処理がダサい。原因もダサい。ただのパラメータ不足だった。
修正前
JavaScript (jQuery)
function update_hoge() {
var params = {foo: "bar"};
$.ajax({
type: "POST",
url: "/index/update_hoge",
data: params,
}).then(function(data){
console.log(JSON.parse(data).text); // <- この一行がダサい
});
}
PHP (FuelPHP)
class Controller_Index extends Controller_Rest {
public function update_hoge() {
$this->format = 'json';
$json = ['text' => 'いろいろ'];
return $this->response($json);
}
}
修正後
$.ajax()
のオプションに dataType:"json"
を書き忘れてた。
JavaScript (jQuery)
function update_hoge() {
var params = {foo: "bar"};
$.ajax({
type: "POST",
url: "/index/update_hoge",
data: params,
dataType: "json",
}).then(function(data){
console.log(data.text); // <- すぐとれる
});
}