Windows Azure上的CORS无法正常工作 [英] CORS on windows azure is not working
本文介绍了Windows Azure上的CORS无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用** AWS实例**在跨浏览器中上传数据成功。但是当我尝试在** windows Azure上执行相同操作时**我收到以下错误:
**跨源请求被阻止:同源策略不允许读取
远程资源。这可以通过将资源移动到同一个域来修复
或启用CORS **
我的Post方法如下:
$。ajax({
type:' POST',
crossDomain: true ,
url:' http://dplannerpython.cloudapp.net:8888/upload',
data:form_data,
contentType : false ,
cache: false ,
processData: false ,
async: true ,
成功: function (数据){
alert( 上传成功!一旦数据配置);
},
错误: function (xhr,ajaxOptions,thrownError),您可以运行作业){
alert(xhr.status);
alert(thrownError);
}
});
解决方案
.ajax({
type:' POST',
crossDomain: true ,
url:' http://dplannerpython.cloudapp.net:8888/upload',
data:form_data,
contentType: false ,
cache: false ,
processData: false ,
a同步: true ,
成功:功能(数据){
alert( 上传成功!一旦数据配置);
},
错误: function (xhr,ajaxOptions,thrownError),您可以运行作业){
alert(xhr.status);
alert(thrownError);
}
});
< blockquote>你还没有共享你的API代码,但检查下面的EnableCors()方法,如果没有实现,那就实现它。
public static class WebApiConfig
{
public static void 注册(HttpConfiguration配置)
{
config.EnableCors();
谢谢
Rushi
< ; system.webServer >
< httpProtocol >
< customHeaders >
< add name = Access-Control-Allow-Origin value = * / >
< add name = Access-Control-Allow-Headers 值 = 内容类型 / >
< add name = 访问控制允许方法 value = GET,POST,PUT,DELETE,OPTIONS / >
< / customHeaders >
< / httpProtocol >
< ModSecurity 已启用 = false configFile = C:\inetpub \wwwroot \owasp_crs \ modsecurity.conf / >
< 验证 validateIntegratedModeConfiguration = false / >
< 处理程序 >
< 删除 name = ExtensionlessUrlHandler-Integrated-4.0 / >
< remove name = OPTIONSVerbHandler / >
< remove name = TRACEVerbHandler / >
< add 名称 = ExtensionlessUrlHandler-Integrated -4.0 path = *。 动词 = * type = System.Web.Handlers.TransferRequestHandler preCondition = integratedMode,runtimeVersionv4.0 / >
< / handlers >
< / system.webServer > 跨度>
I have Uploaded data in cross browser using **AWS instance** sucessfully.But when I am trying to do the same on **windows Azure** I got the following error:
**Cross-Origin Request Blocked: The Same Origin Policy disallows reading the
remote resource. This can be fixed by moving the resource to the same domain
or enabling CORS**
My Post method is given below :
$.ajax({
type: 'POST',
crossDomain: true,
url: 'http://dplannerpython.cloudapp.net:8888/upload',
data: form_data,
contentType: false,
cache: false,
processData: false,
async: true,
success: function (data) {
alert("Upload Successful! You can run a job once the data is configured");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
解决方案
.ajax({ type: 'POST', crossDomain: true, url: 'http://dplannerpython.cloudapp.net:8888/upload', data: form_data, contentType: false, cache: false, processData: false, async: true, success: function (data) { alert("Upload Successful! You can run a job once the data is configured"); }, error: function (xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(thrownError); } });
You have not shared your API Code but check below EnableCors() method and if it is not implemented then implement it.
public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.EnableCors();
Thanks
Rushi
After enabling Cors option please change your web.config as follow.
<system.webServer> <httpProtocol> <customHeaders> <add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Content-Type" /> <add name="Access-Control-Allow-Methods" value="GET, POST, PUT, DELETE, OPTIONS" /> </customHeaders> </httpProtocol> <ModSecurity enabled="false" configFile="C:\inetpub\wwwroot\owasp_crs\modsecurity.conf" /> <validation validateIntegratedModeConfiguration="false" /> <handlers> <remove name="ExtensionlessUrlHandler-Integrated-4.0" /> <remove name="OPTIONSVerbHandler" /> <remove name="TRACEVerbHandler" /> <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" /> </handlers> </system.webServer>
这篇关于Windows Azure上的CORS无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文