admin管理员组文章数量:1323157
I am encountering an issue and I ran out of ideas, I need some guidance towards the origin and/or the solution:
Server Side
I added the standard Microsoft Web Api Controller
class "ValuesController
" which looks like this:
public class ValuesController : ApiController
{
public string Get(int id){ return "value"; }
...
Client Side
In my AngularJS Controller function I have a simple get
$http({method:'GET',url: '/api/values/1'}).success(function(data){
$scope.value =data;
})
The HTML looks like this:
<input type="text" ng-model="value" />
The weird thing(the issue) is that I get: "value" in my input instead of just value (no quotes). To avoid misunderstandings, I am getting this:
Instead of this:
And of course the questions are: why?? and how do I fix it*?
*hopefully the answer will not be returning an object instead of a simple string :)
I am encountering an issue and I ran out of ideas, I need some guidance towards the origin and/or the solution:
Server Side
I added the standard Microsoft Web Api Controller
class "ValuesController
" which looks like this:
public class ValuesController : ApiController
{
public string Get(int id){ return "value"; }
...
Client Side
In my AngularJS Controller function I have a simple get
$http({method:'GET',url: '/api/values/1'}).success(function(data){
$scope.value =data;
})
The HTML looks like this:
<input type="text" ng-model="value" />
The weird thing(the issue) is that I get: "value" in my input instead of just value (no quotes). To avoid misunderstandings, I am getting this:
Instead of this:
And of course the questions are: why?? and how do I fix it*?
*hopefully the answer will not be returning an object instead of a simple string :)
Share edited Feb 4, 2014 at 17:13 Dalorzo asked Feb 4, 2014 at 16:59 DalorzoDalorzo 20k7 gold badges57 silver badges102 bronze badges 1- Your question seems related to this another one: JSON String in response shows up with quotes – Edwin Dalorzo Commented Feb 4, 2014 at 18:29
2 Answers
Reset to default 5I have the impression that this is due to security vulnerabilities in the JSON format. In the documentation of the $http service there is section called JSON Vulnerability Protection that suggests that Angular takes a few precautions to avoid an attack.
They remend the reading of the following article: Anatomy of a Subtle JSON Vulnerability which as far as I can see delves into a case similar to yours.
Bottom line, I think you will have to return a full JSON object and not just a string. An alternative is to make sure you are getting a JSON object, by doing
$scope.value = JSON.parse(value)
The preferred (Angular provided solution) is
$scope.value = angular.fromJson(data);
本文标签: javascriptWebApi Method with single value and AngularJs http json handlingStack Overflow
版权声明:本文标题:javascript - WebApi Method with single value and AngularJs $http json handling - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742143576a2422689.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论