首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法在Live ASP.NET Core 5上显示来自ASP.NET调用的数据

无法在Live ASP.NET Core 5上显示来自ASP.NET调用的数据
EN

Stack Overflow用户
提问于 2022-03-04 11:48:31
回答 2查看 156关注 0票数 0

我的站点上有一个特别有问题的Controller方法。它应该根据产品Id、产品属性即长度和产品属性值(1.5mm)来获取商品的价格。我正在使用一个getJson请求来实现这一点。该方法在我的本地服务器中工作得很好,但是当它在活动服务器上访问该方法时,它并没有提供更高的价格。这个方法看起来像这样。

代码语言:javascript
复制
        [HttpGet]
        [ActionName("GetPriceFromSetAttributes")]
        public IActionResult GetPriceFromSetAttributes(int productId, string productAttributeName, string productAttributeValueName)
        {
            var price = _repository.GetProductPriceFromSetAttributes(productId, productAttributeName, productAttributeValueName);
            return new JsonResult(price);
        }

这是getJson请求。

代码语言:javascript
复制
                var controllerUrl = '@Url.Action("GetPriceFromSetAttributes", "Cart")';
                $.getJSON(controllerUrl, { productId: $("#productId").val(), productAttributeName: $(this).attr('id'), productAttributeValueName: $(this).val() }, function (data) {
                    if (data.attributePrice != null) {
                        $("#attributePrice").val("KES. " + data.attributePrice.toLocaleString() + "");
                        $("#attributePriceToPost").val("" + data.attributePrice + "");
                        $("#originalPriceAt").hide();
                        $("#attributePriceAt").show();
                    }
                    $('#addToCart').html('<i class="fas fa-cart-plus mr-2"></i> Add to Cart');
                    $('#addToCart').prop('disabled', false).removeClass('noHover');
                });

该站点最初抛出了一个跨源异常,在使用下面的代码启用CORS之后,

代码语言:javascript
复制
            services.AddCors(options =>
            {
                options.AddPolicy("MyAllowSpecificOrigins",
                                  builder =>
                                  {
                                      builder.WithOrigins("https://localhost:44383",
                                                          "https://tendcorp.co.ke")
                                                          .AllowAnyHeader()
                                                          .AllowAnyMethod()
                                                          .AllowCredentials();
                                  });
            });
            
            
            

            app.UseCors("MyAllowSpecificOrigins");

没有例外,但问题依然存在。我甚至尝试从dev环境连接到SQL,但都没有效果。

现场服务器和我的本地主机的屏幕截图都附在一起。

有人指指点点吗?我做错什么了?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-03-05 17:21:02

在您的情况下,getjson不是最好的选择,因为它被转换成ajax后,直接尝试jquery。

代码语言:javascript
复制
 var controllerUrl = '@Url.Action("GetPriceFromSetAttributes", "Cart")'
+ '?productId='+ $("#productId").val() 
+'&productAttributeName=' + $(this).attr('id')
+'&productAttributeValueName='+ $(this).val();

  $.ajax({
            type: 'GET',
            dataType: 'json',
            url: controllerUrl,
            success: function (data) {
             if (data.attributePrice != null) {
                .... your code
             }             
          }
  });
票数 2
EN

Stack Overflow用户

发布于 2022-03-17 10:58:30

我使用了Serge的ajax函数(上面标记为答案),并修改了自己的控制器以提供一个字符串,而不是JSON对象,因为活动服务器不断向我的JSON请求发送一个文本响应。

控制器现在看起来如下:

代码语言:javascript
复制
        [HttpGet]
        [ActionName("GetPriceFromSetAttributes")]
        public IActionResult GetPriceFromSetAttributes(int productId, string productAttributeName, string productAttributeValueName)
        {
            var price = _repository.GetProductPriceFromSetAttributes(productId, productAttributeName, productAttributeValueName).AttributePrice.ToString();
            return Content(price);
        }

它也比以前更快地获取所需的数据。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71350978

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档