Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将php数组传递给angularjs服务_Php_Angularjs_Arrays - Fatal编程技术网

如何将php数组传递给angularjs服务

如何将php数组传递给angularjs服务,php,angularjs,arrays,Php,Angularjs,Arrays,我试图在ng click函数中传递一个php数组,以使其在php控制器中 这是我将php数组传递给angular函数的代码 <button type="button" class="btn btn-success" ng-click="removeItem('<?php echo htmlspecialchars(json_encode($value['BookingsHotel']));?>')">Cancel Room</button> 一旦我

我试图在ng click函数中传递一个php数组,以使其在php控制器中

这是我将php数组传递给angular函数的代码

 <button type="button" class="btn btn-success" 
  ng-click="removeItem('<?php echo  htmlspecialchars(json_encode($value['BookingsHotel']));?>')">Cancel Room</button>

一旦我们进行json解码,它就会给出一个空值。

因为您使用的是angular。只需使用$http与PHP后端通信

$http({method: 'POST', url: your_url_variable, contentType: 'application/json; charset=utf-8',
        data: {var1:angular_var1, var2:angular_var2},
        headers: { 'Content-Type': 'application/json; charset=utf-8' }
    }).success(function (result) {
        console.log("result", result);
    }).error(function (data, status, headers, config) {
        console.log('ERROR', data, status, headers, config);
    });
在php页面中

if($_SERVER["REQUEST_METHOD"] == "POST") {
        $postdata = file_get_contents("php://input");
        if(!empty($postdata)) {
            $post = json_decode($postdata);

            $var1 = $post->var1;
            $var2 = $post->var2;

            // do some stuff, then echo the response as json back to your app.

            $result = ["success" => true; "message" => "This is a test message"];
            header('Content-Type: application/json');
            echo json_encode($result);
        }
}

因为你用的是角度。只需使用$http与PHP后端通信

$http({method: 'POST', url: your_url_variable, contentType: 'application/json; charset=utf-8',
        data: {var1:angular_var1, var2:angular_var2},
        headers: { 'Content-Type': 'application/json; charset=utf-8' }
    }).success(function (result) {
        console.log("result", result);
    }).error(function (data, status, headers, config) {
        console.log('ERROR', data, status, headers, config);
    });
在php页面中

if($_SERVER["REQUEST_METHOD"] == "POST") {
        $postdata = file_get_contents("php://input");
        if(!empty($postdata)) {
            $post = json_decode($postdata);

            $var1 = $post->var1;
            $var2 = $post->var2;

            // do some stuff, then echo the response as json back to your app.

            $result = ["success" => true; "message" => "This is a test message"];
            header('Content-Type: application/json');
            echo json_encode($result);
        }
}

你能进一步解释一下你的应用程序中包含的代码在哪里吗?您可以为
$scope.removietem
函数包含代码吗?还要注意
$booked\u rooms=json\u decode(…)作用于原始请求数据。您可能需要:
$booked\u rooms=json\u decode($booked\u rooms)
如果你想让它对
htmlspecialchars\u decode(…)
的结果起作用。Hello@Pravin-如果我的答案对你有帮助,你介意接受吗?谢谢你能进一步解释一下你的应用程序中包含的代码在哪里吗?您可以为
$scope.removietem
函数包含代码吗?还要注意
$booked\u rooms=json\u decode(…)作用于原始请求数据。您可能需要:
$booked\u rooms=json\u decode($booked\u rooms)
如果你想让它对
htmlspecialchars\u decode(…)
的结果起作用。Hello@Pravin-如果我的答案对你有帮助,你介意接受吗?谢谢