我有一个JSON格式字符串,我希望像参数一样添加到帖子请求中。
我正在尝试执行请求,当我执行请求时,我从Web服务中收到此错误:
#0 /opt/assets/WS/ws.php(48): Slim\Slim::handleErrors(8, 'Trying to get p...', '/opt/assets/WS/...', 48, Array)
#1 [internal function]: {closure}()
#2 /opt/assets/WS/vendor/slim/slim/Slim/Route.php(468): call_user_func_array(Object(Closure), Array)
#3 /opt/assets/WS/vendor/slim/slim/Slim/Slim.php(1357): Slim\Route->dispatch()
#4 /opt/assets/WS/vendor/slim/slim/Slim/Middleware/Flash.php(85): Slim\Slim->call()
#5 /opt/assets/WS/vendor/slim/slim/Slim/Middleware/MethodOverride.php(92): Slim\Middleware\Flash->call()
#6 /opt/assets/WS/vendor/slim/slim/Slim/Middleware/PrettyExceptions.php(67): Slim\Middleware\MethodOverride->call()
#7 /opt/assets/WS/vendor/slim/slim/Slim/Slim.php(1302): Slim\Middleware\PrettyExceptions->call()
#8 /opt/assets/WS/ws.php(59): Slim\Slim->run()
#9 {main}
这就像请求不包含真正的JSON对象。我该如何改造它?我做了一些技巧,但似乎不起作用。 那是我的剧本:
assets.controller('AfegirTipusActiusCtrl', function ($scope, $http){
// Camps formulari text pla
/* $scope.nomAtribut = "<input type='text' name='firstname'>";
$scope.mida = "<input type='number' name='firstname'>";
$scope.obligatori = "<input type='checkbox' name='vehicle' value='yes'>"; */
// Construeix combo
$http.get('http://10.0.203.73/WS/ws.php/getCombo/1').success(function(data) {
$scope.options = data;
});
$scope.atributs = [];
$scope.addRow = function(){
var newRow = {
nomAtribut: "",
tipus: "",
mida: "",
prioritat: "",
obligatori: "",
observacions: ""
}
$scope.atributs.push(newRow);
}
/* $scope.addField = function() {
$scope.atributs.push(newRow);
};*/
$scope.prioritat = $scope.atributs.length;
// Envia atributs a WS
$scope.sendRow = function(){
var d = "{'nomAtribut': 'marca','tipus': 'String','mida': '15','prioritat': '1','obligatori': 'No'}";
$http.post("http://10.0.203.73/WS/ws.php/tipusactius/alta", angular.toJson(d)).success(function(data){
$scope.status = data;
})
}
});
编辑:
我还添加了解析请求的php Slim函数:
$app->post('/tipusactius/alta', function () use ($app) {
$json = $app->request->getBody();
header("Content-Type: application/json");
$json_decode = json_decode($json); // $json_decode conte l'bjecte format a partir del json del request
echo $json_decode->{'tipus'};
// $sql = "INSERt INTO atributs_actiu (nomAtribut, midaAtribut, tipus_actius_idtipus_actius, atributObligatori, ordre, tipusAtributs_idTipus, observacions
// , combo_idcombo, combo_codi) values ('".$json_decode->{'nomAtribut'}."', '".$json_decode->{'mida'}."', 1, '0', 1, 1, 'atribut de prova', 1, 1)";
$sql = "INSERt INTO atributs_actiu (nomAtribut, midaAtribut, tipus_actius_idtipus_actius, atributObligatori, ordre, tipusAtributs_idTipus, observacions
, combo_idcombo, combo_codi) values ('".$json_decode->{'nomAtribut'}."', '3', 1, '0', 1, 1, 'atribut de prova', 1, 1)";
$mysqli=getDB();
$result = $mysqli->query($sql);
});
解决: 就像@vidriduch告诉:
$scope.sendRow = function() {
var d = {
'nomAtribut': 'marca',
'tipus': 'String',
'mida': '15',
'prioritat': '1',
'obligatori': 'No'
};
$http.post("http://10.0.203.73/WS/ws.php/tipusactius/alta", angular.toJson(d)).success(function(data) {
$scope.status = data;
})};
答案 0 :(得分:2)
angular.toJson将输入序列化为JSON格式的字符串
https://docs.angularjs.org/api/ng/function/angular.toJson
在你的情况下,你正在序列化字符串......
您的代码可能如下所示:
$scope.sendRow = function() {
var d = {
'nomAtribut': 'marca',
'tipus': 'String',
'mida': '15',
'prioritat': '1',
'obligatori': 'No'
};
$http.post("http://10.0.203.73/WS/ws.php/tipusactius/alta", angular.toJson(d)).success(function(data) {
$scope.status = data;
})};
答案 1 :(得分:1)
如果您想将JSON
字符串转换为JavaScript
对象,则应使用angular.fromJson()
method。
另外,请务必将'
中的单引号"
转换为双引号JSON
:
// ' becomes " in your string
var d = '{"nomAtribut": "marca","tipus": "String","mida": "15","prioritat": "1","obligatori": "No"}';
$http.post("http://10.0.203.73/WS/ws.php/tipusactius/alta", angular.fromJson(d))
.success(function(data){
$scope.status = data;
}
答案 2 :(得分:0)
确保您的JSON参数未被任意引用锁定。
将JSON字符串分配给变量并直接传递它将在这里工作。