Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/371.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
Javascript 通过jquery.get()将php数组传递给脚本_Javascript_Php_Jquery - Fatal编程技术网

Javascript 通过jquery.get()将php数组传递给脚本

Javascript 通过jquery.get()将php数组传递给脚本,javascript,php,jquery,Javascript,Php,Jquery,在我的脚本中,我有一行 $scope.items = [{"id":"1","name":"name 1","description":"description 1","field3":"field3 1","field4":"field4 1","field5 ":"field5 1"}]; 我制作了一个data.php文件来模拟上面显示的$scope.items的数组值 <?php getaccessories = array("id"=>"1","name"=>"nam

在我的脚本中,我有一行

$scope.items = [{"id":"1","name":"name 1","description":"description 1","field3":"field3 1","field4":"field4 1","field5 ":"field5 1"}];
我制作了一个data.php文件来模拟上面显示的$scope.items的数组值

<?php
getaccessories = array("id"=>"1","name"=>"name 1","description"=>"description 1","field3"=>"field3 1","field4"=>"field4 1","field5 "=>"field5 1");

echo json_encode($getaccessories);
?>
@sergiu,这是你让我为data.php做的事情,请看我是否做对了

<?php

// $getaccessories = array("id"=>"1","name"=>"name 1","description"=>"description 1","field3"=>"field3 1","field4"=>"field4 1","field5 "=>"field5 1");

$getaccessories = array();
$accessory = new stdClass();
$accessory->id = "1";
$accessory->name = "name 1";
$accessory->description = "description 1";
$accessory->field3 = "field3 1";
$accessory->field4 = "field4 1";
$accessory->field5 = "field5 1";

$getaccessories[] = $accessory;


echo json_encode($getaccessories);
?>
下面是$scope.items和$get()

我希望这对您可能需要的帮助有所帮助

$.get("data.php", function(data){
    $scope.items = data;
}, "json");
$.get的第二个参数是回调,就像在您收到我从服务器请求的数据时为我回调一样

我还猜你在
getaccessories
前面有
$

您需要一个对象数组。 您可以这样构建它:

$getaccessories = array();
$accessory = new stdClass();
$accessory->id = "1";
$accessory->name = ="name 1";
...

$getaccessories[] = $accessory;

谢谢,我尝试了你的解决方案,但没有收到任何效果。可能json_encode($getaccessories)没有接收到的$scope.items的正确数组组成?请参阅我的更新答案。你可能没有正确地构建它。k tnx,现在尝试一下……没有用。请检查这一点,看看是否有一个来自data.php的数组用于$scope.items。我看不出客户端AngularJS代码有任何问题。调试并找出
json\u encode($getaccessories)
是否与
$scope.items
的外观相似。
Object {readyState: 1, setRequestHeader: function, getAllResponseHeaders: function, getResponseHeader: function, overrideMimeType: function…}
    $$v: Array[1]
        0: Object
            age: 50
            name: "Moroni"
        __proto__: Object
        length: 1
        __proto__: Array[0]
$.get("data.php", function(data){
    $scope.items = data;
}, "json");
$getaccessories = array();
$accessory = new stdClass();
$accessory->id = "1";
$accessory->name = ="name 1";
...

$getaccessories[] = $accessory;