我正在使用net 2.1和angular 6编写我的第一个应用程序。我能够使用C#将来自net 2.1的json数据发布到Visual Studio的iis Express网页上。
我能够从前端(角度/典型)将数据发布到服务器。我对数据执行计算,并将json结果发布在iis express服务器网页上。如何使用angular获取数据?
这是我的Angular api服务文件,允许我将其发布到服务器
export class ApiService {
private handleError: HandleError;
constructor(
private http: HttpClient,
httpErrorHandler: HttpErrorHandler) {
this.handleError = httpErrorHandler.createHandleError('HeroesService');
}
stock: StockComponent ;
stockURL = 'https://localhost:44310/api/stock'
/** POST: */
postStock (stock) {
console.log('stock is ', stock);
this.http.post(this.stockURL, stock).subscribe(res => {
console.log(res)
})
}
getBuySellData(){
return this.http.get('https://localhost:44310/api/stock');
}
}
这是组件文件:
export class StockComponent {
stock = {}
constructor(private api: ApiService){
}
post(stock){
console.log('this is the stock ', stock)
this.api.postStock(stock)
}
这是Visual Studio中控制器的一部分
[HttpPost]
public async Task<IActionResult> Post([FromBody]Models.Stock stock)
{
_context.Stocks.Add(stock);
await _context.SaveChangesAsync();
return Ok(stock);
}
[HttpGet]
public IEnumerable<DateCloseBuySell> GetQuote()
{
string responseString = string.Empty;
答案 0 :(得分:0)
我在Xamarin.Forms中遇到了类似的麻烦。
为解决这个问题,我做了类似的事情:
private const string Url = "https://localhost:44310/api/stock";
private HttpClient _client = new HttpClient();
protected async void OnGetList()
{
if (CrossConnectivity.Current.IsConnected)
{
try
{
var content = await _client.GetStringAsync(Url);
var list = JsonConvert.DeserializeObject<List<Model>>(content);
}
catch (Exception e)
{
Debug.WriteLine("", e);
}
}
}
答案 1 :(得分:0)
我将api.service.ts修改为
stock: StockComponent ;
stockURL = 'https://localhost:44310/api/stock'
/** POST: */
postStock (stock) {
this.http.post(this.stockURL, stock).subscribe(res => {
console.log(res)
})
}
getBuySellData(){
this.http.get(this.stockURL).subscribe(res => {
console.log(res)
})
和component.ts到
constructor(private api: ApiService){
}
ngOnInit() {
this.api.getBuySellData()
}
post(stock){
this.api.postStock(stock)
}
我在客户端控制台窗口中看到json数据。谢谢克里斯。