12

I've searched for hours for this and can't find a single thing that answers the question. I've created and published a new Azure Machine Learning service, and have created an endpoint. I can call the service using the Postman REST CLient, but accessing it via a JavaScript webpage returns a console log saying that CORS is enabled for the service. Now, for the life of me, I can't figure out how to disable CORS for Azure Machine Learning services. Any help would be much appreciated, thanks!

Dan Ciborowski - MSFT
  • 6,196
  • 8
  • 48
  • 78
Neil
  • 135
  • 1
  • 9
  • My problem is not with constructing a query (I'm using JS, not ruby), but rather the CORS limitations on azure – Neil May 20 '15 at 07:33
  • https://books.google.com/books?id=fserCAAAQBAJ&pg=PT156&lpg=PT156&dq=machine+learning+azure+CORS&source=bl&ots=mksmcZc2qC&sig=zwyoVdxjspZVA5RZIYWxbQoj9Jw&hl=en&sa=X&ei=-VxdVZHuN4HMsAWe7YDwDQ&ved=0CDMQ6AEwAQ#v=onepage&q=machine%20learning%20azure%20CORS&f=false – Tyrion Lannister May 21 '15 at 04:21
  • Do you mind if I see a sample query with JavaScript? – Tyrion Lannister May 28 '15 at 02:53
  • I used the sample C# code they provided to create this: http://govmind.azurewebsites.net/ but I don't like C#. I'd prefer they give sample code in JS but they don't – Tyrion Lannister May 28 '15 at 02:54

3 Answers3

4

Currently, we don't support disabling CORS on API side but you can either use the above option or you can use the API management service to disable CORS. The links below should help you with this

Here are the links: step by step guide, also this video on setting headers, and this doc on policies.

API Management service allow CORS by enabling it in the API configuration page

neerajkh
  • 1,231
  • 8
  • 9
  • 4
    Although this answer solves the OP issue, the fact that an Azure ML web service already published (and costing real money) has to be wrapped in an Azure API management service (that costs even more money) just to overcome a missing feature in the underlying Azure ML service (disabling CORS) is morally repugnant at best. – tatlar Jun 16 '17 at 19:36
1

You have to start your browser with --disable-web-security ( Chrome that is ). Here's some jQuery that allowed me to call the service AFTER re-starting my browser with --disable-web-security:

$(document).ready(function () {
    var ajaxData = "-- the request body ";
    var serviceUrl = "https://ussouthcentral.services.azureml.net/workspaces/00e36959fc3e4673a32eae9f9b184346/--whatever";

    $.ajax({
        type: "POST",
        url: serviceUrl,
        data: ajaxData,
        headers: {
            "Authorization": "Bearer --API KEY HERE--",
            "Content-Type": "application/json;charset=utf-8"
        }
    }).done(function (data) {
        console.log(data);
    });
});

That returned the data. NOTE: Be sure you see that warning in Chrome. I didn't at fist, because some Chrome processes were still running in the background. After killing those, restarting with that flag, seeing the message, it worked. ( Chrome v40.something )

See: https://stackoverflow.com/a/6083677/896697

Community
  • 1
  • 1
Jochen van Wylick
  • 4,940
  • 4
  • 36
  • 58
  • -1: The OP asked specifically "I can't figure out how to disable CORS for Azure Machine Learning services". Disabling your local browser security is (inherently) not secure and not a solution for a production application that would use the Azure ML published web-service. – tatlar Jun 16 '17 at 19:21
1

Just an excerpt from the Azure ML Book (one may find it useful):

This CORS restriction really means that if you wish to fully exploit Azure Machine Learning web services for deployment, testing, and production for a wide variety of (web) clients, you will need to host your own server-side applications. You basically have two choices.

  1. Host a web application, such as an ASP.NET webpage, and invoke the Azure Machine Learning web service server-side to conform to the current Azure Machine Learning CORS restrictions.
  2. Host your own web service that does provide CORS support and can in turn invoke the Azure Machine Learning web service on behalf of a wide variety of web and mobile clients via modern protocols and data formats like REST and JSON.
Alibek Jakupov
  • 560
  • 4
  • 13