简体   繁体   English

CORS无法与Azure移动服务.NET后端一起使用

[英]cors not working with azure mobile service .net backend

we are running into a permissions problem and what ever page we look at we can't seem to get it working. 我们遇到了权限问题,无论浏览什么页面,似乎都无法正常运行。 We have two parts. 我们分为两个部分。 The mobile service in Azure and the webpage (client). Azure和网页(客户端)中的移动服务。 Webservice is called " https://mobileservice.azure-mobile.net/tables/program .... and the client webpage is called " http://azure-webservicesclient.azurewebsites.net ". Web服务称为“ https://mobileservice.azure-mobile.net/tables/program ....”,客户端网页称为“ http://azure-webservicesclient.azurewebsites.net ”。

We enabled on " https://mobileservice.azure-mobile.net " 我们在“ https://mobileservice.azure-mobile.net ”上启用了

<system.webServer>
   <httpProtocol>
     <customHeaders>
       <add name="Access-Control-Allow-Origin" value="*" />
     </customHeaders>
   </httpProtocol>
 <system.webServer>

But this does not seem to work, it still give a error: 404 (not found) No access-control-allow-origin' header is present on the requested source. 但这似乎不起作用,它仍然会给出错误:404(未找到)在请求的源上不存在access-control-allow-origin'标头。 We also followed the tutorial " http://www.codeguru.com/csharp/.net/net_asp/using-cross-origin-resource-sharing-cors-in-asp.net-web-api.html " but same result. 我们还遵循了教程“ http://www.codeguru.com/csharp/.net/net_asp/using-cross-origin-resource-sharing-cors-in-asp.net-web-api.html ”,但结果相同。

Please see here our code, any one any ideas what is wrong? 请在这里查看我们的代码,任何人有什么想法吗?

    <!-- WinJS code -->
    <script src='http://ajax.aspnetcdn.com/ajax/mobileservices/MobileServices.Web-1.1.2.min.js'></script>
    <script>
    $(function () {var client = new WindowsAzure.MobileServiceClient('https://mobileservice.azure-mobile.net/', '*****KEY********'),

        todoItemTable = client.getTable('program');
       console.log(todoItemTable.read());

            // Read current data and rebuild UI.
            // If you plan to generate complex UIs like this, consider using a JavaScript templating library.
            function refreshTodoItems() {
                var query = todoItemTable.where({ id_program: 21 });

                query.read().then(function (todoItems) {
                    //var listItems = $.map(todoItems, function (item) {
                    //    return $('<li>')
                    //        .attr('data-todoitem-id', item.id)
                    //        .append($('<button class="item-delete">Delete</button>'))
                    //        .append($('<div>').append($('<input class="item-text">').val(item.text)));
                    //});
                    console.log(todoItems);
                    //$('#todo-items').empty().append(listItems).toggle(listItems.length > 0);
                    //$('#summary').html('<strong>' + todoItems.length + '</strong> item(s)');
                }, handleError);

            }



            // On initial load, start by fetching the current data
        //    refreshTodoItems();
        });
    </script>

To enable CORS with Azure Mobile Services, you use the ASP.NET Web API CORS NuGet package: 要为Azure移动服务启用CORS,请使用ASP.NET Web API CORS NuGet包:

http://www.nuget.org/packages/Microsoft.AspNet.WebApi.Cors/5.1.2

At the moment you have to enable it using this trick: 目前,您必须使用以下技巧启用它:

https://gist.github.com/HenrikFrystykNielsen/6c934be6c6c8fa9e4bc8

We will soon provide baked-in support for CORS so that you won't have to enable it yourself. 我们很快将为CORS提供内置支持,因此您不必自己启用它。

Hope this helps, 希望这可以帮助,

Henrik 亨里克

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM