【问题标题】:how to display selected data from json object如何显示从 json 对象中选择的数据
【发布时间】:2017-07-31 11:52:57
【问题描述】:

当我运行这个函数时,我得到了一个 JSON 对象,如图所示 我只想显示一个选定的参数,比如用户名。

 [displaying json object on running this function][1] 

$scope.signUp = function () {
    $http({
        method: "post",
        dataType:'json',
        url: "http://192.168.0.60/api/Customer/CustomerLoginService",
        crossDomain : true,
        data: {
            'username': $scope.username,
            'password': $scope.password,
             'entrytoken': $scope.entrytoken               
        },
     headers: { 'Accept':' text/plain','Content-Type': 'application/json' }
    }).then(function success(data) {
        alert("success function");
       var obj=JSON.stringify(data);
       alert(obj);                 
   });

如何显示单个参数。

【问题讨论】:

  • 你不需要对数据进行字符串化,只需做 data.username
  • @Lekens 输出未定义
  • 执行此操作以检查 json 中的元素,....console.log('DATA=',data);打开控制台查看您的数据可能存在用户名

标签: angularjs json cordova


【解决方案1】:
then(function success(data) {        
       alert(data.data.username);
               });

【讨论】:

    【解决方案2】:
    alert(obj.data.username);
    

    可以像访问任何普通对象一样访问对象属性。 但在未来,不需要对返回进行字符串化。

    【讨论】:

    • 它给出了一个未定义的输出
    • 如果你只是警告'obj',输出是什么?
    • 尝试警报(obj.data.username);
    • @AmruthaJRaj 很高兴你找到了答案。
    【解决方案3】:
    headers: { 'Accept':' text/plain','Content-Type': 'application/json' }
        }).then(function success(data) {
            alert("success function");
          // var obj=JSON.stringify(data);
           console.log('DATA=',data);
    

    这将允许您检查用户名是否存在于作为响应的数据返回中。打开浏览器控制台以查看数据。请使用谷歌浏览器

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-04
      • 1970-01-01
      • 2016-05-10
      • 1970-01-01
      相关资源
      最近更新 更多