我正在对cakephp上的控制器进行$ .ajax调用,以将一些数据发送到控制器,然后使用该数据连接到第三方API并创建链接,$。ajax调用按预期工作我得到了帖子数据,但是调用第三方API时出现了问题,
public function createLinks() {
if ($this->request->is('ajax') ) {
$this->autoRender = false;
}
if ($this->request->isPost()) {
$link = $this->request->data['spotifyLink'];
$token = $this->request->data['accessToken'];
$boardID = $this->request->data['boardID'];
$trackTitle = $this->request->data['trackTitle'];
}
$apiLink = 'https://api.linkfire.com/campaigns/boards/'.$boardID.'/links';
$body = array("track" => $trackTitle,"baseUrl" => $link);
$http = new Client();
$response = $http->post($apiLink, $body,
['headers' =>['Authorization' => 'Bearer '.$token,
'content-type' => 'application/json']]);
echo json_encode($response);
}
这就是我要回来的
{"readyState":4,"responseText":"{}","responseJSON":{},"status":200,"statusText":"OK"}
我的问题是我可以从ajax调用中接收发布数据并在同一函数中进行http-> post调用吗?上面的代码中是否有任何错误,因为$ http-> post调用似乎无法正常工作,我们将不胜感激。
如果我更改echo json_encode($ response);返回$ response;我收到500个内部服务器错误,并在日志文件中显示
Controller actions can only return Cake\Http\Response or null