Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.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/4/json/15.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 如何处理空JSON响应?_Javascript_Json - Fatal编程技术网

Javascript 如何处理空JSON响应?

Javascript 如何处理空JSON响应?,javascript,json,Javascript,Json,这是调用Web服务时得到的JSON响应。如果响应为空,我需要打印不显示任何内容 [] 这就是我现在使用的代码。但它不起作用。有人请给我一些建议。你可以用这样的东西 $http({ method:'POST', url: 'http://xxxxxx', headers: { Content-Type: 'application/json', Accept: 'application/json'

这是调用Web服务时得到的JSON响应。如果响应为空,我需要打印不显示任何内容

[]

这就是我现在使用的代码。但它不起作用。有人请给我一些建议。

你可以用这样的东西

$http({ 
        method:'POST', 
        url: 'http://xxxxxx',
        headers: { 
           Content-Type: 'application/json', 
           Accept: 'application/json'
        }, 
        data: JSON.stringify(x)
}).then(function (response) { 
        $scope.details=response.data;
        if($scope.invoiceDetails == null){
           console.log("nothing to show");
        }
    });

您正在检查if条件中的其他变量

if (response.data.length === 0)
{
    console.log("nothing to show");
}
$http({method:'POST',
网址:'http://xxxxxx',
标题:{
“内容类型”:“应用程序/json”,
“接受”:“应用程序/json”
}, 
数据:JSON.stringify(x)
}).then(函数(响应){
$scope.details=response.data;
如果(!$scope.details){
console.log(“无需显示”);
}
或
//如果您的详细信息是数组
if(!$scope.details | |$scope.details.length)

类似这样的内容

什么是
typeof response.data
response.data.length==0
?警报(typeof response.data);is**object**我能知道我为什么被否决吗?如果你否决了投票,请留下一个理由。试试你自己的答案。@Durga更新了答案?我应该检查它有什么用?@Durga所以我也在这样做,检查我的答案。//如果你的详细信息是数组,如果(!$scope.details | |$scope.details.length
!$scope.details
将始终为false,那么为什么要检查?重复和不带描述这在所有情况下都不起作用。如果未定义详细信息,则将出现类型错误,无法读取null的属性“length”…如果响应为
[]
,则不会未定义。
$http({ 
        method:'POST', 
        url: 'http://xxxxxx',
        headers: { 
           Content-Type: 'application/json', 
           Accept: 'application/json'
        }, 
        data: JSON.stringify(x)
}).then(function (response) { 
        $scope.details=response.data;
        if($scope.invoiceDetails == null){
           console.log("nothing to show");
        }
    });
if ($scope.details.length === 0)
{
    console.log("nothing to show");
}
if (response!=undefined)
{
    //your code here
}
else{
console.log("nothing to show");
}