【问题标题】:500 internal server error when sending data to controller and try to make an API call向控制器发送数据并尝试进行 API 调用时出现 500 内部服务器错误
【发布时间】:2018-07-12 07:36:36
【问题描述】:

我正在尝试进行 $.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.

请求网址:/src/Controller/PagesController/createLinks 参考网址:http://localhost:8765/

任何帮助将不胜感激。

【问题讨论】:

  • 500 是内部服务器错误。查看日志文件以获取更多信息
  • 网址“/src/Controller/PagesController/createLinks”是否正确?不应该是“/create-links”吗?
  • @jbe 我只是改变它,现在我得到 404 (Not Found)- 在日志中我得到 Controller class Src could not be found
  • “页面/创建链接”?
  • @jbe 500 内部服务器错误消失了,现在我得到 [Cake\View\Exception\MissingTemplateException] 模板文件“Pages/json/create_links.ctp”丢失。

标签: jquery ajax cakephp internal-server-error


【解决方案1】:

ajax 调用“/src/Controller/PagesController/createLinks”中的 url 应该是 “页面/创建链接”。

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

【讨论】:

    猜你喜欢
    • 2017-05-17
    • 1970-01-01
    • 2023-03-20
    • 2020-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-20
    相关资源
    最近更新 更多