CORS与蔚蓝的移动服务.NET后端工作 [英] cors not working with azure mobile service .net backend

查看:218
本文介绍了CORS与蔚蓝的移动服务.NET后端工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在运行到权限问题,似乎什么都页我们看一下,我们没有得到它的工作。我们有两部分。在Azure中的移动服务和网页(客户端)。 Web服务被称为 https://mobileservice.azure-mobile.net/tables/program ....和客户端的网页被称为 http://azure-webservicesclient.azurewebsites.net

我们的 https://mobileservice.azure-mobile.net

启用

 < system.webServer>
   < httpProtocol>
     < customHeaders>
       <添加名称=访问控制允许来源VALUE =*/>
     < / customHeaders>
   < / httpProtocol>
 < system.webServer>

不过,这似乎并没有工作,但它仍然给出一个错误:404(未找到)没有访问控制允许来源标头请求的源present。我们也跟着教程<一个href=\"http://www.$c$cguru.com/csharp/.net/net_asp/using-cross-origin-resource-sharing-cors-in-asp.net-web-api.html\" rel=\"nofollow\">http://www.$c$cguru.com/csharp/.net/net_asp/using-cross-origin-resource-sharing-cors-in-asp.net-web-api.html\"但同样的结果。

请在这里看到我们的code,任何一个任何想法有什么不好?


        

 &LT;! -  WinJS code  - &GT;
    &LT;脚本src='http://ajax.aspnetcdn.com/ajax/mobileservices/MobileServices.Web-1.1.2.min.js'></script>
    &LT;脚本&GT;
    $(函数(){VAR的客户=新WindowsAzure.MobileServiceClient('https://mobileservice.azure-mobile.net/','***** KEY ********),        todoItemTable = client.getTable(计划);
       的console.log(todoItemTable.read());            //读取当前数据和重建UI。
            //如果您打算生成这样复杂的用户界面,可以考虑使用一个JavaScript库模板。
            功能refreshTodoItems(){
                变种查询= todoItemTable.where({id_program:21});                query.read()。然后(功能(todoItems){
                    // VAR时listItems = $ .MAP(todoItems,函数(项目){
                    //返回$('&LT;立GT;')
                    // .attr('数据内的TodoItem-ID',item.id)
                    // .append($('&LT;按钮类=项,删除&GT;删除&LT; /按钮&GT;'))
                    // .append($('&LT; D​​IV&GT;')追加($('&LT;输入类=项目文本&GT;')。VAL(item.text)));
                    //});
                    的console.log(todoItems);
                    //$('#todo-items').empty().append(listItems).toggle(listItems.length大于0);
                    //$('#summary').html('<strong>'+ todoItems.length +'&LT; / STRONG&GT;项目(S)');
                }的HandleError);            }            //初始加载,通过获取当前的数据开始
        // refreshTodoItems();
        });
    &LT; / SCRIPT&GT;


解决方案

要启用的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支持,这样你就不必自己启用它。

希望这有助于

亨里克

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). Webservice is called "https://mobileservice.azure-mobile.net/tables/program.... and the client webpage is called "http://azure-webservicesclient.azurewebsites.net".

We enabled on "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. 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.

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:

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.

Hope this helps,

Henrik

这篇关于CORS与蔚蓝的移动服务.NET后端工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆