向控制器发送数据并尝试进行 API 调用时出现 500 内部服务器错误
500 internal server error when sending data to controller and try to make an API call
我正在尝试进行 $.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.
请求URL:/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
推荐人 URL:http://localhost:8765/
任何帮助将不胜感激。
ajax调用“/src/Controller/PagesController/createLinks”中的url应该是
"pages/create-links".
另外,看看 json 浏览量:https://book.cakephp.org/3.0/en/views/json-and-xml-views.html
我正在尝试进行 $.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.
请求URL:/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 推荐人 URL:http://localhost:8765/
任何帮助将不胜感激。
ajax调用“/src/Controller/PagesController/createLinks”中的url应该是 "pages/create-links".
另外,看看 json 浏览量:https://book.cakephp.org/3.0/en/views/json-and-xml-views.html