Javascript SyntaxError:Object.parse(本机)AngularJS 中的意外标记 o

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29763557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:56:08  来源:igfitidea点击:

SyntaxError: Unexpected token o at Object.parse (native) AngularJS

javascriptjsonangularjsangularjs-http

提问by blue piranha

Question from AngularJS noob.

来自 AngularJS 菜鸟的问题。

I am trying to use an asmx web service to display grid. I tested the web service and it correctly outputs the JSON data. Here is my controller

我正在尝试使用 asmx Web 服务来显示网格。我测试了 Web 服务,它正确输出了 JSON 数据。这是我的控制器

app.controller('SetupController', ['$scope', '$http', function ($scope, $http) {

    var url = 'app/pricefilessetup/grid.asmx/getGridJson';

    $http.get(url).success(function (data) {
        var myjson = JSON.parse(data);
        $scope.products= JSON.parse(myjson);
    });
}]);

For some reason, SO is not allowing me to paste the html but it basically has a ng-controller directive and ng-repeat to loop through the JSON data.

出于某种原因,SO 不允许我粘贴 html,但它基本上有一个 ng-controller 指令和 ng-repeat 来循环遍历 JSON 数据。

When I run this web app, I get the error

当我运行此网络应用程序时,出现错误

SyntaxError: Unexpected token o at Object.parse (native) and it points to following line

SyntaxError:Object.parse(本机)中的意外标记 o,它指向以下行

  $scope.questions = JSON.parse(myjson);

I tried checking the value of myjson using alert and it displays [object Object], [object Object], ...

我尝试使用警报检查 myjson 的值,它显示 [object Object]、[object Object]、...

Is there anything I am missing here

有什么我在这里想念的吗

采纳答案by Pankaj Parkar

I think data returned is already in JSON, no need of JSON.parse(), unless it in string format.

我认为返回的数据已经是 JSONJSON.parse()格式,不需要,除非它是字符串格式。

$scope.products= data;

回答by Ramesh Rajendran

Why you using JSON.parsein two times?

为什么你用JSON.parse了两次?

 var myjson = JSON.parse(data);
  $scope.products = JSON.parse(myjson);

You have already parse the data object,So then why you parsing another one time?

你已经解析了数据对象,那为什么还要解析一次呢?

also i think your data is return Json result, so you don't need to parse the object

我也认为你的数据是返回 Json 结果,所以你不需要解析对象

just use this

就用这个

$scope.products = data;

回答by fatCop

Your variable myjsonis already a valid JavaScript Object. You do not have to use JSON.parse on it.

您的变量myjson已经是一个有效的 JavaScript 对象。您不必在其上使用 JSON.parse。

回答by Mohammad nasim

Simple solution, just use an absolute url:

简单的解决方案,只需使用绝对网址:

var url = 'http://demo/app/pricefilessetup/grid.asmx/getGridJson';

Instead of using var url = 'app/pricefilessetup/grid.asmx/getGridJson';I have checked.

而不是使用var url = 'app/pricefilessetup/grid.asmx/getGridJson';我检查过。

回答by vinesh

In my case it was string literal being passed in as parameter to JSON.parse().

在我的情况下,它是字符串文字作为参数传递给JSON.parse().

For Example JSON.parse('asdf')would throw an error Uncaught SyntaxError: Unexpected token a.

例如JSON.parse('asdf')会抛出错误Uncaught SyntaxError: Unexpected token a

With specific case, in Single page angular application, the access token was being passed to the JSON.parse()and clearing the cookies in browser solved the problem for me.

在特定情况下,在单页角度应用程序中,访问令牌被传递给JSON.parse()并清除浏览器中的 cookie 为我解决了这个问题。