Angular2で投稿リクエストを作成しようとしています。何らかの理由で私はPOST要求をしません。私はクライアントとサーバーの両方をGETリクエストに変更し、それ以外は同じものを残しました。正常に動作します。だから私はPOSTの要求を行うことができるようにヘッダを別の方法を構成することができますかと思います。
更新
は、だから私はそれを実行するために得ることができたが、今場所のパラメータはnull見せているHTTP POSTはASP.NETには実行されませんが、GETリクエストは
クライアント
search(latitude: any, longitude: any){
let headers = new Headers();
headers.append('Content-Type', 'application/json');
let body = JSON.stringify({ latitude, longitude });
this.http.post('/api/SampleData/CurrentForecasts',body, { headers: headers })
.map(response => response.json())
.subscribe(
data => this.Debug(data),
err => console.log("Error: \n"+err),
() => console.log('Get Complete')
);
}
サーバー
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using System.Net.Http;
using System.Net.Http.Headers;
namespace Weather.Controllers
{
[Route("api/[controller]")]
public class SampleDataController : Controller
{
private static string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
[HttpPost("[action]")]
public async Task<IActionResult> CurrentForecasts(string location)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(" https://api.forecast.io/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP GET
HttpResponseMessage response = await client.GetAsync("forecast/APIKEY/"+location);
if (response.IsSuccessStatusCode)
{
var forecast = await response.Content.ReadAsStringAsync();
return Content(forecast, "application/json");
}
}
return Json(null);
}
[HttpGet("[action]")]
public async Task<IActionResult> SendRequest()
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(" https://api.forecast.io/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
// HTTP GET
HttpResponseMessage response = await client.GetAsync("forecast/APIKEY/37.8267,-122.423");
if (response.IsSuccessStatusCode)
{
var forecast = await response.Content.ReadAsStringAsync();
return Content(forecast, "application/json");
}
}
return Json(null);
}
}
}
エラー 場所[ヌル]
サーバー側のルーティング設定はどのようになっていますか? –
[Route( "api/[controller]")] – John
POSTパラメータを除いて、同じコードのGET要求が機能します。だから私はそれを疑う。 – John