gpt4 book ai didi

javascript - AngularJS 意外的字符串

转载 作者:可可西里 更新时间:2023-11-01 17:07:47 24 4
gpt4 key购买 nike

按照 Angular 书中的示例,他们有以下代码:

<script>
var test = {};


var app = angular.module("app",[]);

app.run(function($http){
$http.get("/file").success(function(data){
test.input = data;
});
});

app.controller("ctrl",function($scope){
$scope.handle = test;
});


</script>

大概是从文件中提取数据,将其推送到对象“test”,然后将其放入作用域的句柄变量中。然而,当我写这篇文章时,问题是 $http.get:

   app.run(function($http){
$http.get("/file").success(function(data){
test = data;
});
});

产生以下错误:

SyntaxError: Unexpected string at Object.parse (native) at vc (http://localhost/common/js/frameworks/angular.min.js:15:480) at Zb (http://localhost/common/js/frameworks/angular.min.js:82:229) at http://localhost/common/js/frameworks/angular.min.js:83:143 at m (http://localhost/common/js/frameworks/angular.min.js:7:322) at dd (http://localhost/common/js/frameworks/angular.min.js:83:125) at d (http://localhost/common/js/frameworks/angular.min.js:84:380) at http://localhost/common/js/frameworks/angular.min.js:119:113 at n.a.$get.n.$eval (http://localhost/common/js/frameworks/angular.min.js:133:221) at n.a.$get.n.$digest (http://localhost/common/js/frameworks/angular.min.js:130:233)
(anonymous function) b.$get (anonymous function)
a.$get.n.$eval a.$get.n.$digest a.$get.n.$apply h K
z.onload

对我来说,这表明 $http 不需要字符串 url。有人可以向我解释一下吗?这本书的示例代码是错误的,还是已经过时了?

编辑:

/文件内容如下:

[{"a":"1","b":"2"},{"aa":"3","bb":"4"}]

编辑 2:

来自其他 SO 文章 Problems parsing a JSON response using AngularJS看起来 $http.get 应该是自动解析的

最佳答案

您应该对数据进行编码。并且错误将消失:

'[{"a":"1","b":"2"},{"aa":"3","bb":"4"}]'

关于javascript - AngularJS 意外的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33621501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com