向控制器发送数据并尝试进行API调用时出现500个内部服务器错误

时间:2018-07-12 07:36:36

标签: jquery ajax cakephp internal-server-error

我正在尝试进行$ .ajax调用并将一些数据发送到cakephp中的控制器并进行API调用,但我不断收到500个内部服务器错误。

jquery文件

$.ajax({
type: 'POST',
url: '/src/Controller/PagesController/createLinks',
data: {
    spotifyLink: spotifyLink,
    accessToken: access_token,
    boardID: boardID,
},
dataType: 'json',
complete: function (response) {
    console.log("data coming back from pagesController.php" + response.responseText);
},
error: function (jqXHR, textStatus, errorThrown) {
    console.log(jqXHR, textStatus, errorThrown);
}
});

控制器文件

public function createLinks() {

    if ($this->request->is('ajax') && $this->request->is('post') ){

        $link = $_POST['spotifyLink'];
        $token = $_POST['access_token'];
        $boardID = $_POST['boardID'];

        $apiLink = 'https://api.linkfire.com/campaigns/boards/'.$boardID.'/links';

        $body = array("baseUrl" => $link);

        $http = new Client();
        $response = $http->post($apiLink, $body,
            ['headers' =>['Authorization' => 'Bearer '.$token, 
                          'content-type' => 'application/json']]);
        $json = $response->json;
        echo ($body);
        return $response;
    }

我的路由器文件中有

$routes->connect('/src/Controller/PagesController/createLinks', ['controller' => 'Pages', 'action' => 'createLinks']);

这是我得到的错误

jquery-3.3.1.min.js:2 POST http://localhost:8765/src/Controller/PagesController/createLinks 500 (Internal Server Error)

请注意,数据已在控制台和响应正文中打印出来,我有以下内容

{"message":"An Internal Error Has Occurred.","url":"\/src\/Controller\/PagesController\/createLinks","code":500}

日志文件显示以下错误:

Error: [Cake\Routing\Exception\MissingControllerException] Controller class Js could not be found.

请求网址:/js/MDB/js/bootstrap.min.js.map

2018-07-12 07:23:17 Error: [LogicException] Controller actions can only return Cake\Http\Response or null.

请求URL:/ src / Controller / PagesController / createLinks 推荐连结网址:http://localhost:8765/

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

ajax调用“ / src / Controller / PagesController / createLinks”中的网址应为 “页面/创建链接”。

另外,请查看json视图:https://book.cakephp.org/3.0/en/views/json-and-xml-views.html