Angularjs 将对象列表从Angular传递到Web API

Angularjs 将对象列表从Angular传递到Web API,angularjs,json,asp.net-web-api,Angularjs,Json,Asp.net Web Api,如何将对象列表从Angular传递到Web API 我尝试将内容类型用作application/json,但它显示了CORS飞行前错误 以下是Web API Post方法: [HttpPost] public bool getDetails(List<Subnets> data) { //Logic here } 以下是子网类: public class Subnets { public string ID { get; set; } p

如何将对象列表从Angular传递到Web API

我尝试将内容类型用作application/json,但它显示了CORS飞行前错误

以下是Web API Post方法:

 [HttpPost]
  public bool getDetails(List<Subnets> data)
  {
      //Logic here
  }
以下是子网类:

 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; }
}
我尝试使用“application/x-www-form-urlencoded”作为内容类型;字符集=UTF-8'。它调用API操作方法,但参数“data”为空。内容类型为“application/json”,显示CORS飞行前错误

已阻止跨源请求:同一源策略不允许读取 位于的远程资源 . (理由:CORS 标题“访问控制允许来源”不匹配(null)

提前谢谢

解决方案:

我在global.asax中添加了以下代码,它起了作用:

 protected void Application_BeginRequest()
    {
        if (Request.Headers.AllKeys.Contains("Origin") && Request.HttpMethod == "OPTIONS")
        {
            Response.Flush();
        }
    }

CORS不允许应用程序/json。你对文本/纯文本没问题

现在甚至可以避免使用标题部分。正如您所说,它调用API操作方法,但数据为空。jsondata可能不是有效的对象

请尝试使用以下代码

 $http({
        method: "POST",
        url: "http://localhost:60262/api/IPUpload/getDetails",
        data: JSON.stringify(jsondata)        
    }).then(function (data) {

public bool getDetails(List<Subnets> data)
{
}
$http({
方法:“张贴”,
url:“http://localhost:60262/api/IPUpload/getDetails",
数据:JSON.stringify(jsondata)
}).then(功能(数据){
公共bool getDetails(列表数据)
{
}
没有[HttpPost]


我还想看看您的子网类和jsondata

如果我没记错,Angular.js会在您使用
application/json
时触发飞行前请求。您的web API需要以200响应响应飞行前请求

如果您正在编写自己的API,请确保在
http://localhost:60262/api/IPUpload/getDetails
endpoint


在您启用
选项并返回HTTP方法后,200 Angular将按预期工作。

您需要在web API前端启用CORS。默认情况下,angularjs支持跨域ajax调用。但如果服务器不允许跨域调用,它将失败

您可以按如下方式启用CORS:

  • 在服务器端启用选项调用
  • 对于每个实际的post端点,您还需要一个选项端点,这样当浏览器检查CORS并发送选项调用时,它应该返回200
  • 跨域ajax调用也支持application\json

  • 您使用
    application/json
    的方法是正确的,但是您需要启用CORS。请参阅ASP.NET Web API下的内容。我在控制器上添加了以下属性--[EnableCors(源代码:,标题:“内容类型”,方法:*”]。仍然不走运。我不认为CORS是你的问题。我们需要看看你的jsondata是什么样子,我们需要看看应该接收请求的api代码。我猜你的模型与api期望的不匹配。另外,向GET端点发出POST请求…不,不。如果它不是GET,那么不要调用它getDetailsremoved[httppost],仍然不起作用。它甚至没有调用Web API操作方法。还有其他建议吗?我在Web.config中添加了以下自定义标题,仍然没有成功:在Web.config以及global.asax文件上尝试过这样做,仍然不起作用。你能发布错误的屏幕截图和确切的ajax吗呼叫?我可以试着在我的网页上运行它,帮助你更好。
     $http({
            method: "POST",
            url: "http://localhost:60262/api/IPUpload/getDetails",
            data: JSON.stringify(jsondata)        
        }).then(function (data) {
    
    public bool getDetails(List<Subnets> data)
    {
    }