2

I wanted to know if there is a way to call the Azure Machine Learning webservice using JavaScript Ajax.

The Azure ML gives sample code for C#, Python and R.

I did try out to call the webservice using JQuery Ajax but it returns a failure.

I am able to call the same service using a python script.

Here is my Ajax code :

  $.ajax({
        url: webserviceurl,
        type: "POST",           
        data: sampleData,            
        dataType:'jsonp',                        
        headers: {
        "Content-Type":"application/json",            
        "Authorization":"Bearer " + apiKey                       
        },
        success: function (data) {
          console.log('Success');
        },
        error: function (data) {
           console.log('Failure ' +  data.statusText + " " + data.status);
        },
  });
Jesse
  • 3,522
  • 6
  • 25
  • 40
Tilak
  • 81
  • 7
  • What failure does it return? You're looking at the statusText and the status. What do they say? Does the console display any errors? If you pay attention to the second and third arguments of the `error` function, do they say anything useful? What does the Network tab of your browser's developer tools say? Is the request being made? Does it get a response? – Quentin May 25 '16 at 18:37
  • I get error 404. After researching some more I found out that CORS is not supported by Azure ML. [link](https://social.msdn.microsoft.com/Forums/vstudio/en-US/b6ddeb77-30e1-45b2-b7c1-eb4492142c0a/azure-ml-published-web-services-cross-origin-requests?forum=MachineLearning) . – Tilak May 25 '16 at 18:55

3 Answers3

3

It doesn't work due to CORS not enabled on Azure ML web services. You can wrap Azure ML web service using Azure API management which can be called from JavaScript - https://azure.microsoft.com/en-us/documentation/articles/api-management-get-started/

neerajkh
  • 1,233
  • 8
  • 9
2

Well after a lot of RnD, I was able to finally call Azure ML using some workarounds.

Wrapping Azure ML webservice on Azure API is one option.

But, what I did was that I created a python webservice which calls the Azure webservice.

So now my HTML App calls the python webservice which calls Azure ML and returns data to the HTML App.

Tilak
  • 81
  • 7
0

Per my experience, it seems to be caused by the timeout, so I think you can try to refer to the answer for the SO thread Set timeout for ajax (jQuery) to set the timeout for the ajax request.

Hope it help.


There is an answered SO thread Azure Machine Learning - CORS which is similar with your issue. I think it's helpful, please try.

Community
  • 1
  • 1
Peter Pan
  • 23,476
  • 4
  • 25
  • 43
  • I did give it a try but I don't think that is a real issue. At first I was getting CORS error, to resolve this I added datatype as jsonp. Now I am getting a 404 error code. Is there anything which can be done from Azure ML web service setting? – Tilak May 25 '16 at 18:32
  • @Tilak Hi, I updated my post, please try. And any update, please feel free to let me know. – Peter Pan May 31 '16 at 07:48
  • @PeterSmith Yes..that is what I figured out that Azure ML does not support CORS – Tilak May 31 '16 at 18:07