2014-10-10 5 views
6

IE8 में और 9 मैं जब मैं एक CORS WebAPI कॉल कर रहा हूँ निम्नलिखित JavaScript त्रुटि हो रही है:IE9, AngularJS CORS साथ IE8 रिटर्न "प्रवेश निषेध है" - ASP.NET WebAPI

Error: Access is denied. 
{ 
    [functions]: , 
    description: "Access is denied.", 
    message: "Access is denied.", 
    name: "Error", 
    number: -2147024891 
} 

मैं सेट मेरी WebAPI यहाँ वर्णित की तरह http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

तो WebAPI शामिल हैं:

public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      config.EnableCors(new EnableCorsAttribute("*", "*", "*")); 
     [...] 

मेरा परीक्षण AngularJS ऐप:

<!DOCTYPE html> 
    <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ng="http://angularjs.org" ng-app="app"> 
    <head> 
     <title>test</title> 
     <script src="Scripts/angular.js"></script> 
     <script src="app.js"></script> 
    </head> 
    <body> 
     <div ng-controller="testController as vm"> 
      {{vm.test}} 
      {{vm.data}} 
     </div> 
    </body> 
    </html> 

app.js:

var app = angular.module('app'); 

    app.controller('testController', function ($http) { 
     var vm; 
     vm = this; 

     vm.test = "bla non no "; 
     vm.data = null; 

     $http.defaults.headers.common['Authorization'] = 'a token' 

     return $http({ 
      method: 'GET', 
      data: null, 
      url: 'http://webapi.com/api/controller/getactionmethod/', 
     }, function (data) { 
      console.log("bla"); 
     }).success(function (data, status, headers, config) { 
      console.log("bla a"); 
      vm.data; 
     }); 


    }); 

ऊपर कोड/WebAPI क्रोम और IE 10 IE10 प्रिंट के साथ काम करता है कहता है:

SEC7118: के लिए http://webapi.com/api/controller/getactionmethod/ आवश्यक क्रॉस उत्पत्ति रिसोर्स शेयरिंग (CORS) XMLHttpRequest। SEC7119: http://webapi.com/api/controller/getactionmethod/ के लिए XMLHttpRequest आवश्यक सीओआरएस प्रीफलाइट।

मैं वास्तव में अटक गया हूं और नहीं जानता कि मैं और क्या कोशिश कर सकता हूं। कोई विचार?

+0

http://stackoverflow.com/questions/10232017/ie9-jquery-ajax-with-cors-returns-access-is-denied – epascarello

+1

मैं angularjs का उपयोग नहीं कर रहा हूं jQuery। – Briefkasten

+2

आईई 8 और आईई 9 मानक कोर एक्सएचआर का समर्थन नहीं करते हैं। अधिक जानकारी के लिए इस प्रश्न का मेरा उत्तर देखें http://stackoverflow.com/questions/25141650/xdomainrequest-vs-xmlhttprequest – MrCode

उत्तर

2

कोणीय जेएस v1.2.23 आईई 8 या आईई 9 के लिए सीओआरएस अनुरोधों का समर्थन नहीं करता है। लेकिन IE8/9 XDomainRequest ऑब्जेक्ट के साथ सीमित CORS का समर्थन करता है। यह भी देखें http://msdn.microsoft.com/en-us/library/ie/cc288060(v=vs.85).aspx

मैं यहाँ descriped http://samuellam.wordpress.com/2013/08/03/ie-89-cors-support-in-angular-js/

तरह AngularJS lib को संशोधित करने की कोशिश की लेकिन मैंने देखा मैं साथ XDomainRequest एक कस्टम हेडर का अनुरोध करता है नहीं भेज सकते। इसलिए मैंने उसी मशीन पर उसी मशीन पर प्रोजेक्ट को तैनात कर दिया जो आईई 8 और 9 के लिए काम करेगा, जो वास्तव में केवल एक कामकाज है।

http://blogs.msdn.com/b/ieinternals/archive/2010/05/13/xdomainrequest-restrictions-limitations-and-workarounds.aspx

2

मैं जब CORS अनुरोध कर IE8/9 (ASP.NET के बजाय Django बैकएंड) के साथ एक ही मुद्दा था।

इस समस्या को हल करने के कई तरीके हैं। मेरे लिए सबसे आसान और सबसे तेज़ समाधान पॉलीफिल का उपयोग jpillora से करना था। इस पॉलीफिल के साथ सामान्य CORS XMLHttpRequests IE8/9 पर XDR के लिए बदल दिया जाएगा।

XHook को शामिल करें और निम्नलिखित जोड़ने से पहले हुक आपकी साइट के लिए:

कई अन्य समाधान
xhook.before(function(request, callback) { 
    //skip browsers that dont use XDR 
    if(!window.XDomainRequest) 
    return callback(); 
    //skip requests that aren't cross domain 
    var url = request.url; 
    var loc = window.location; 
    var hostname = loc.hostname + (loc.port ? ":"+loc.port : ""); 
    if(!/^https?:\/\/([^\?\/]+)/.test(url) || RegExp.$1 === hostname) 
    return callback(); 

    //if not GET, force POST 
    var method = request.method; 
    if(method !== 'GET') method = 'POST'; 
    //force same protocol 
    url = url.replace(/^https?:/,loc.protocol); 
    //request! 
    var xdr = new window.XDomainRequest(); 
    xdr.timeout = request.timeout; 
    //proxy events 
    var proxy = function(e) { 
    xdr['on'+e] = function() { 
     request.xhr.dispatchEvent(e); 
    }; 
    }; 
    var events = ['progress','timeout','error']; 
    for(var i = 0; i < events.length; ++i) 
    proxy(events[i]); 
    //custom onload 
    xdr.onload = function() { 
    callback({ 
     status: 200, 
     statusText: "OK", 
     headers: { 
     'Content-Type': xdr.contentType 
     }, 
     text: xdr.responseText 
    }) 
    }; 
    xdr.open(method, url); 
    xdr.send(request.body); 
    return 
}); 

हैं:

संबंधित मुद्दे