簡體   English   中英

如何將JSON數據從C#控制器傳遞到angular js?

[英]How to pass json data from C# controller to angular js?

如何將JSON數據從C#控制器傳遞到angular 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;    

    }

但無法在angular js控制器中獲取以下數據是控制台中引發的錯誤,“錯誤:JSON.parse:預期屬性名稱或'}'在JSON數據的第1行第2列

如何解決這個問題? 這里有什么問題?

我懷疑是因為您的JSON格式。 您使用的是單引號還是雙引號。 有效的JSON使用雙引號

這是我運行您的json時得到的信息,我已將響應更改為HttpResponseMessage並明確設置了響應內容類型以消除此問題。 基於您在Java腳本方面的錯誤,我認為您的問題是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;
        }

結果是:

破滅

雖然雙引號:

     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;
    }

正常工作:

正確

使用EF創建json對象並使用Web api控制器獲取(選擇),放置(更新),發布(插入),刪除(刪除)數據

 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;
        }
    } 
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM