如何修复No' Access-Control-Allow-Origin'标头出现在请求的资源上

时间:2016-09-16 08:25:16

标签: javascript jquery ajax asp.net-mvc api

我知道这个问题已被问过多次,但我仍然没有找到解决办法 当我尝试从客户端与我的API通信时,我遇到了跨域错误。我不知道是否 这里缺少一些东西。

XMLHttpRequest无法加载http://localhost:28184/Customer/AddCustomer

  

否'访问控制 - 允许 - 来源'标题出现在请求的上   资源。因此不允许来源http://localhost:28189   访问。响应的HTTP状态代码为404.

Ajax电话:

 function AddCompany()
 {
   var companyModel = {
      ----------
 }

$.support.cors = true;
$.ajax({    
    url: BaseUrl + "Customer/AddCustomer",
    type: "POST",
    withCredentials: false,    
    dataType: "Jason",
    cach: false,
    async: true,
    crossDomain: true,
    data: companyModel,
    success: function(data){
       alert("Company has been successfully registered");
    }       
 });
}

API控制器:

using System.Threading.Tasks;
using System.Web.Http;
using System.Web.Http.Cors;
using Fleet_API.Models;

namespace Fleet_API.Controllers
{
[EnableCors(origins: "*", headers: "*", methods: "*")]
[RoutePrefix("Api/Customer")]
public class CustomerController : ApiController
{
    [Route("AddCustomer")]
    [HttpPost]
    public async Task<bool>AddCompany(CustomerModel model)
    {
         ------------------------
    }
}
}

从Windows

托管文件
   # localhost name resolution is handled within DNS itself.
   #    127.0.0.1       localhost
   #    ::1             localhost

0 个答案:

没有答案