首页 > 解决方案 > 如何接收用 angularjs 填充并由 ajax 发送到服务器端(PHP)的 json 对象?

问题描述

我有以下 json 对象:

$scope.sellAccessories=
 [
 {"id": "1","name": "aa", "quantity": "3","total_price": "100"}
 ,
 {"id": "2","name": "bb", "quantity": "4","total_price": "200"}
 ,
 {"id": "3","name": "cc", "quantity": "5","total_price": "300"}
];

我使用ajax发送对象如下:

var options={
    type : "get",
    url : "../php/sell.php",
    data: {"function":"sellAccess","data":$scope.sellAccessories},
    dataType: 'json',
    async : false,
    cache : false,
    success : function(response,status){
        alert("success")
    },
    error:function(request,response,error){
        alert("error")
    }
};
$.ajax(options);

我尝试使用$_GET['name']接收数据,但没有成功

我的PHP代码:

$item_name=json_decode($_GET['name']);

我也尝试过: $data=json_decode($_GET['data']);

但它们都不起作用!

提前致谢

标签: phpjsonajaxangularjs-scopejsondecoder

解决方案


您需要根据 url 查询检索数据。因此,例如,如果您需要 JSON 数据,那么您可以通过以下方式检索 JSON

$jsonData = $_GET['data'];

然后你必须处理这个 JSON 对象,你可以json_decode像这样使用 PHP 函数使它成为一个数组

$arrayData = json_decode($jsonData, true);

然后,您可以像任何其他数组一样遍历所述数据。


推荐阅读