检测到损坏的拦截器:拒绝中未提供配置对象:
Broken interceptor detected: Config object not supplied in rejection:
检测到损坏的拦截器:拒绝中未提供配置对象:
我在调用 post 方法时遇到此错误。
我已提供服务、控制器和 PHP 代码
服务
angular.module('sbAdminApp')
.factory('Branch', function($resource){
return $resource('api/branchdetails/:branch_id',{branch_id:'@_branch_id'},{
update: {
method: 'PUT'
}
});
})
.service('popupService',function($window){
this.showPopup=function(message){
return $window.confirm(message);
}
});
控制器
angular.module('sbAdminApp')
.controller('BranchDetailsController', function($scope,$state,$stateParams,$window,Branch){
$scope.branch = new Branch();
$scope.addBranch=function(){
$scope.branch.$save(function(){
$state.go('branchdetails');
});
}
});
PHP代码
<?php
require_once('Slim/Slim.php');
require_once('dbconnection.php');
$app = new Slim();
$app->post('/branchdetails','addBranch');
$app->run();
function addBranch() {
$request = Slim::getInstance()->request();
$branch = json_decode($request->getBody());
$sql = "INSERT INTO branch(branch_name, branch_address, branch_phno, branch_mobileno, branch_contactperson, branch_createdate, branch_modifieddate) VALUES (:branch_name,:branch_address, :branch_phno, :branch_mobileno, :branch_contactperson, :branch_createdate, :branch_modifieddate)";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("branch_name", $branch->branch_name);
$stmt->bindParam("branch_address", $branch->branch_address);
$stmt->bindParam("branch_phno", $branch->branch_phno);
$stmt->bindParam("branch_mobileno", $branch->branch_mobileno);
$stmt->bindParam("branch_contactperson", $branch->branch_contactperson);
$stmt->bindParam("branch_createdate", $branch->branch_createdate);
$stmt->bindParam("branch_modifieddate", $branch->branch_modifieddate);
$stmt->execute();
$branch->branch_id = $db->lastInsertId();
$db = null;
echo json_encode($branch);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
?>
在您的代码中的某处,您有一个 interceptor
for $httpProvider
,它没有正确的响应错误部分,如下所示:
(function () {
angular.module('App')
.config(['$httpProvider', httpProviderConfig]);
function httpProviderConfig($httpProvider) {
var interceptor = ['$rootScope', '$q', function ($rootScope, $q) {
return {
'responseError': function (rejection) {
return $q.reject(rejection);
}
};
}];
$httpProvider.interceptors.push(interceptor);
}
})();
这种类型的错误通常也发生在 api 没有返回有效的 JSON 时。如果 Content-Type header 是 application/json,或者响应看起来像一个有效的 JSON-stringified,AngularJS transform Response 将尝试将响应解析为 JSON object 或数组。首先始终尝试从后端接收有效的 JSON object,其次还要处理回调函数中的错误。这是一个很好的做法。如果您仍然没有收到有效的 JSON 那么在错误回调中处理它也可以解决问题
https://code.angularjs.org/1.7.6/docs/api/ng/service/$http#default-transformations
notificationsGET: function () {
return Restangular.one('getusermenu.php').get({
menu: 'notification',
token: sessionStorage.getItem('userToken')
}).then(function (data) {
// success callback
return data;
}, function (err) {
// error callback
console.log('error is ', err);
}
);
}
检测到损坏的拦截器:拒绝中未提供配置对象: 我在调用 post 方法时遇到此错误。
我已提供服务、控制器和 PHP 代码
服务
angular.module('sbAdminApp')
.factory('Branch', function($resource){
return $resource('api/branchdetails/:branch_id',{branch_id:'@_branch_id'},{
update: {
method: 'PUT'
}
});
})
.service('popupService',function($window){
this.showPopup=function(message){
return $window.confirm(message);
}
});
控制器
angular.module('sbAdminApp')
.controller('BranchDetailsController', function($scope,$state,$stateParams,$window,Branch){
$scope.branch = new Branch();
$scope.addBranch=function(){
$scope.branch.$save(function(){
$state.go('branchdetails');
});
}
});
PHP代码
<?php
require_once('Slim/Slim.php');
require_once('dbconnection.php');
$app = new Slim();
$app->post('/branchdetails','addBranch');
$app->run();
function addBranch() {
$request = Slim::getInstance()->request();
$branch = json_decode($request->getBody());
$sql = "INSERT INTO branch(branch_name, branch_address, branch_phno, branch_mobileno, branch_contactperson, branch_createdate, branch_modifieddate) VALUES (:branch_name,:branch_address, :branch_phno, :branch_mobileno, :branch_contactperson, :branch_createdate, :branch_modifieddate)";
try {
$db = getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("branch_name", $branch->branch_name);
$stmt->bindParam("branch_address", $branch->branch_address);
$stmt->bindParam("branch_phno", $branch->branch_phno);
$stmt->bindParam("branch_mobileno", $branch->branch_mobileno);
$stmt->bindParam("branch_contactperson", $branch->branch_contactperson);
$stmt->bindParam("branch_createdate", $branch->branch_createdate);
$stmt->bindParam("branch_modifieddate", $branch->branch_modifieddate);
$stmt->execute();
$branch->branch_id = $db->lastInsertId();
$db = null;
echo json_encode($branch);
} catch(PDOException $e) {
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
}
?>
在您的代码中的某处,您有一个 interceptor
for $httpProvider
,它没有正确的响应错误部分,如下所示:
(function () {
angular.module('App')
.config(['$httpProvider', httpProviderConfig]);
function httpProviderConfig($httpProvider) {
var interceptor = ['$rootScope', '$q', function ($rootScope, $q) {
return {
'responseError': function (rejection) {
return $q.reject(rejection);
}
};
}];
$httpProvider.interceptors.push(interceptor);
}
})();
这种类型的错误通常也发生在 api 没有返回有效的 JSON 时。如果 Content-Type header 是 application/json,或者响应看起来像一个有效的 JSON-stringified,AngularJS transform Response 将尝试将响应解析为 JSON object 或数组。首先始终尝试从后端接收有效的 JSON object,其次还要处理回调函数中的错误。这是一个很好的做法。如果您仍然没有收到有效的 JSON 那么在错误回调中处理它也可以解决问题 https://code.angularjs.org/1.7.6/docs/api/ng/service/$http#default-transformations
notificationsGET: function () {
return Restangular.one('getusermenu.php').get({
menu: 'notification',
token: sessionStorage.getItem('userToken')
}).then(function (data) {
// success callback
return data;
}, function (err) {
// error callback
console.log('error is ', err);
}
);
}