AngularからWeb APIへオブジェクトのリストを渡すにはどうすればいいですか?AngularからWeb APIへのオブジェクトのリストの受け渡し
application/jsonとしてコンテンツタイプを使用しようとしましたが、CORSプリフライトエラーが表示されます。サブネットクラス
$scope.save = function (jsondata) {
$http({
method: "POST",
url: "http://localhost:60262/api/IPUpload/getDetails",
data: jsondata,
headers: {
'Content-Type': 'application/json'
}
}).then(function (data) {
alert('success');
if (data.status) {
$scope.msg = "Data has been inserted ! ";
}
else {
$scope.msg = "Error : Something Wrong";
}
}, function (error) {
alert('fail');
$scope.msg = "Error : Something Wrong";
})
}
以下の通りである:
public class Subnets
{
public string ID { get; set; }
public string Subnet { get; set; }
public string IPAddress { get; set; }
public string ServerName { get; set; }
public string Mac { get; set; }
public string Vmware { get; set; }
public string Usage { get; set; }
public string Owner { get; set; }
public DateTime ExpiryDate { get; set; }
public string Email { get; set; }
public string SwitchIP { get; set; }
public string SwitchPort { get; set; }
public string PortVLAN { get; set; }
public string Zone { get; set; }
public string Justification { get; set; }
public string CustomerID { get; set; }
public string Remarks { get; set; }
}
私はのContentを使用してみました
[HttpPost]
public bool getDetails(List<Subnets> data)
{
//Logic here
}
後は角度コードである:以下の
はウェブAPIポストメソッドであります'application/x-www-form-urlencoded;と入力してください。 charset = UTF-8 '。これはAPIアクションメソッドを呼び出しますが、パラメータ "data"は空です。 content-typeが 'application/json'の場合、CORSプリフライトエラーが表示されます。ブロックされた
クロスオリジン・リクエスト:同一生成元ポリシーが http://localhost:60262/api/StaticIPUpload/getDetailsで リモートリソースを読ん禁止します。 (理由:CORS ヘッダー 'Access-Control-Allow-Origin'が '(null)'と一致しません)。
ありがとうございます。
ソリューション:私はGlobal.asaxの中で次のコードを追加し、それが働いた
:
protected void Application_BeginRequest()
{
if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
{
Response.Flush();
}
}
あなたは 'アプリケーション/ json'と正しい軌道に乗っているが、あなたはCORSを有効にする必要があります。 ** ASP.NET Web API **の[here](https://enable-cors.org/server_aspnet.html)を参照してください。 –
Iは、コントローラの属性次の追加した - [EnableCors(起源: "HTTP:// localhostを:51099"、ヘッダー: "コンテンツタイプ"、メソッド: "*")]。まだ運がありません。 – Skye
私はCORSがあなたの問題だとは思わない。あなたのjsondataがどのように見えるかを調べる必要があり、要求を受け取るはずのapiコードを見る必要があります。私はあなたのモデルが、APIが期待しているものと一致しないと推測しています。また、POST要求をGETエンドポイントに発行する...大きな問題はありません。それはそれは削除さ –