Access control for cross site requests in Internet Explorer

后端 未结 6 2383
谎友^
谎友^ 2020-12-28 21:20

I am trying to make an AJAX call from several domains to a single one which will handle the request. Enabling Cross domain in Firefox and Chrome was easy by setting the head

6条回答
  •  天涯浪人
    2020-12-28 22:20

    Much has changed since I first posted my solution for CORS in IE7 and above. First of all the jQuery property $.support.cors is true by default, .NET frameworks 4.0 and above no longer support CORS as implemented in versions 3.5.1 and lower. When writing a web service with ASP.NET 4.0 I had install the Thinktecture.IdentityModel which is available as a NuGet package. Then, in the Application_Start method of the Global.asax file, add:

    void Application_Start(object sender, EventArgs e) 
    { 
        Thinktecture.IdentityModel.Http.Cors.IIS.UrlBasedCorsConfiguration.Configuration
            .ForResources("*")
            .ForOrigins("http://localhost:80, http://mydomain")
            .AllowAll()
            .AllowMethods("GET", "POST");
    }
    

    Now add the httpProtocol element within system.webServer:

    
      
        
        
        
      
    
    

    My $.ajax call is now:

    $.ajax({
        url: serviceUrl, 
        data: JSON.stringify(postData),
        type: "POST",
        cache: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: onSuccess,
        error: onError
    });
    

提交回复
热议问题