如何将J#数据从C#控制器传递给角度js?

时间:2015-07-06 15:41:27

标签: javascript c# json asp.net-mvc angularjs

如何将J#数据从C#控制器传递给角度js?我想将json从控制器传递给角度js。我尝试了不同,但没有一个工作。请参阅下面的代码,

var app = angular.module('myApp', []);
app.controller('customersCtrl', function ($scope, $http) {
    $http.get("/adnActions/getJson")
    .success(function (response) { alert(response.records); $scope.names = response.records; });
});

C#控制器代码

 public async Task<string> getJson()
    {
                 data="{'records':[ {'Name':'Alfreds Futterkiste','City':'Berlin','Country':'Germany'}, {'Name':'Ana Trujillo Emparedados y helados','City':'México D.F.','Country':'Mexico'}]}";
       return data;    

    }

但无法获得角度js控制器中的数据 下面是控制台中出现的错误, &#34;错误:JSON.parse:预期的属性名称或&#39;}&#39;在JSON数据的第1行第2行

如何解决这个问题?问题是什么?

2 个答案:

答案 0 :(得分:1)

我怀疑这是因为你的JSON的格式。您使用单引号与双引号。有效的JSON使用double quotes

以下是我运行json时得到的内容我已将响应更改为HttpResponseMessage并明确设置响应内容类型以消除此问题。根据您在Javascript方面的错误,我认为您的问题是您的JSON中的单引号。

    public async Task<HttpResponseMessage> GetJsonSingle()
    {
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StringContent("{'records':[ {'Name':'Alfreds Futterkiste','City':'Berlin','Country':'Germany'}, {'Name':'Ana Trujillo Emparedados y helados','City':'México D.F.','Country':'Mexico'}]}")
        };

        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        return result;
        }

结果:

busted

双引号:

     public async Task<HttpResponseMessage> GetJsonDouble()
    {
        HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new StringContent("{\"records\":[ {\"Name\":\"Alfreds Futterkiste\",\"City\":\"Berlin\",\"Country\":\"Germany\"}, {\"Name\":\"Ana Trujillo Emparedados y helados\",\"City\":\"México D.F.\",\"Country\":\"Mexico\"}]}")
        };

        result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        return result;
    }

正常工作:

correct

答案 1 :(得分:0)

使用EF创建json对象并使用web api控制器进行GET(选择),PUT(更新),POST(插入),删除(删除)数据

 public class CarsController : ApiController
    {
        private static readonly ICarRepository _cars = new CarRepository();
        // GET api/<controller>
        public IEnumerable<Car> Get()
        {
            return _cars.GetAllCars();
        }

        // GET api/<controller>/5
        public Car Get(int id)
        {
            Car c = _cars.GetCar(id);
            if (c == null)
                throw new HttpResponseException(HttpStatusCode.NotFound);
            return c;
        }

        // POST api/<controller>
        public Car Post(Car car)
        {
            return _cars.AddCar(car);
        }

        // PUT api/<controller>/5
        public Car Put(Car car)
        {
            if (!_cars.Update(car))
                throw new HttpResponseException(HttpStatusCode.NotFound);
            return car;
        }

        // DELETE api/<controller>/5
        public Car Delete(int id)
        {
            Car c = _cars.GetCar(id);
            _cars.Remove(id);
            return c;
        }
    } 
}