Azure ML 在不能配置跨域的情况下如何被前端调用

本文介绍如何解决AzureML生成的WebService跨域错误,通过创建后端桥梁实现前端AJAX请求,避免直接跨域调用。文章详细展示了使用ASP.NET作为中间层,转发请求至AzureML的服务实例,并提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目前Azure ML 生成的web service如果通过前端直接ajax的话,会出现跨域400错,所以我们需要一个后台桥梁,

先访问后端桥梁,再访问Azure ML 的webservice 

 $.ajax({
                type: "POST",
                url: "../api/Values/post1",
                data:   { "": $("#cv").val() },
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    alert(data);
                    layer.close(index);
                },
                error: function (xhr, state, errorThrown) {
                    requesFail(xhr);
                    alert("fail");
                    layer.close(index);
                }
            });
 [HttpPost]
        // POST api/values
        public Task<string> Post1([FromBody]string value)
        {
           
          return  InvokeRequestResponseService(value);
        }
  static async Task<string> InvokeRequestResponseService(string data)
        {
            using (var client = new HttpClient())
            {
                var scoreRequest = new
                {
                    Inputs = new Dictionary<string, StringTable>() {
                        {
                            "cv",
                            new StringTable()
                            {
                                ColumnNames = new string[] {"CV"},
                                Values = new string[,] {  { data } }
                            }
                        },
                    },
                    GlobalParameters = new Dictionary<string, string>()
                    {
                    }
                };
                const string apiKey = "abc"; // Replace this with the API key for the web service
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", apiKey);

                client.BaseAddress = new Uri("https://abc.com");
                HttpResponseMessage response = await client.PostAsJsonAsync("", scoreRequest).ConfigureAwait(false);
                if (response.IsSuccessStatusCode)
                {
                    string result = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
                    return result;
                }
                else
                {
                    string responseContent = await response.Content.ReadAsStringAsync();
                    return responseContent;
                }
            }
        }


  public class StringTable
    {
        public string[] ColumnNames { get; set; }
        public string[,] Values { get; set; }
    }

如果从ASP.Net应用程序的UI线程调用此代码,则下面的“await”语句可能会导致死锁。
 解决此问题的一种方法是调用ConfigureAwait(false)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值