【100分】WebApi内置跨域功能,为什么小弟我就实现不了?速度来高手帮帮小弟我!
【100分】WebApi内置跨域功能,为什么我就实现不了??????速度来高手帮帮我!!!!!
如题,我按网上给的方法,用System.Web.Http.Cors做webapi跨域的设置,但还是无法跨域访问,
代码如下
启动跨域:
控制器配置
我的开发平台是vs2013,请问有没有谁有这方面的经验,请赐教一二,万分没有,只好百分感谢!!谢谢!!
------解决思路----------------------
在Web.config的system.webServer配置节下增加配置
------解决思路----------------------
如果通过jquery 跨域访问,在服务器端和客户端需要分别配合:
1. 在服务器端,需要为每一个请求进行授权。例如在消息头中要有
Access-Control-Allow-Origin:*
2. 在客户端要写
jQuery.support.cors = true;
如题,我按网上给的方法,用System.Web.Http.Cors做webapi跨域的设置,但还是无法跨域访问,
代码如下
启动跨域:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http.Formatting;
using System.Web.Http;
namespace WebApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
//开启跨域设置
config.EnableCors();
//GlobalConfiguration.Configuration.EnableCors();
//返回json格式的控制
GlobalConfiguration.Configuration.Formatters.JsonFormatter.MediaTypeMappings.Add(new QueryStringMapping("json", "true", "application/json"));
}
}
}
控制器配置
using System.Web.Http;
using System.Web.Http.Cors;
namespace WebApi.Controllers
{
[EnableCors(origins: "http://localhost:40226", headers: "*", methods: "*")]
public class MouldController : ApiController
{
[ApiVisitFilter]
public MouldModel Get([FromUri]ParaModel model)
{
var mould = new MouldModel();
// 。。。。。。
return mould;
}
}
}
我的开发平台是vs2013,请问有没有谁有这方面的经验,请赐教一二,万分没有,只好百分感谢!!谢谢!!
------解决思路----------------------
在Web.config的system.webServer配置节下增加配置
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Headers" value="*" />
<add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE" />
</customHeaders>
</httpProtocol>
------解决思路----------------------
如果通过jquery 跨域访问,在服务器端和客户端需要分别配合:
1. 在服务器端,需要为每一个请求进行授权。例如在消息头中要有
Access-Control-Allow-Origin:*
2. 在客户端要写
jQuery.support.cors = true;