首页 文章

cors不使用azure移动服务.net后端

提问于
浏览
0

我们遇到了一个权限问题,我们看到的页面似乎无法让它运行起来 . 我们有两个部分 . Azure中的移动服务和网页(客户端) . Web服务被称为“https://mobileservice.azure-mobile.net/tables/program ....并且客户端网页被称为”http://azure-webservicesclient.azurewebsites.net“ .

我们启用了“https://mobileservice.azure-mobile.net

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

但这似乎不起作用,它仍然会给出错误:404(未找到)请求的源上没有access-control-allow-origin'标头 . 我们也遵循教程“http://www.codeguru.com/csharp/.net/net_asp/using-cross-origin-resource-sharing-cors-in-asp.net-web-api.html”但结果相同 .

请看这里我们的代码,任何一个想法有什么问题?

<!-- 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>

1 回答

  • 1

    要使用Azure移动服务启用CORS,请使用ASP.NET Web API CORS NuGet包:

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

    目前你必须使用这个技巧启用它:

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

    我们很快将为CORS提供烘焙支持,这样您就不必自己启用它 .

    希望这可以帮助,

    亨里克

相关问题