我的站点上有一个特别有问题的Controller方法。它应该根据产品Id、产品属性即长度和产品属性值(1.5mm)来获取商品的价格。我正在使用一个getJson请求来实现这一点。该方法在我的本地服务器中工作得很好,但是当它在活动服务器上访问该方法时,它并没有提供更高的价格。这个方法看起来像这样。
[HttpGet]
[ActionName("GetPriceFromSetAttributes")]
public IActionResult GetPriceFromSetAttributes(int productId, string productAttributeName, string productAttributeValueName)
{
var price = _repository.GetProductPriceFromSetAttributes(productId, productAttributeName, productAttributeValueName);
return new JsonResult(price);
}这是getJson请求。
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之后,
services.AddCors(options =>
{
options.AddPolicy("MyAllowSpecificOrigins",
builder =>
{
builder.WithOrigins("https://localhost:44383",
"https://tendcorp.co.ke")
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
});
});
app.UseCors("MyAllowSpecificOrigins");没有例外,但问题依然存在。我甚至尝试从dev环境连接到SQL,但都没有效果。
现场服务器和我的本地主机的屏幕截图都附在一起。


有人指指点点吗?我做错什么了?
发布于 2022-03-05 17:21:02
在您的情况下,getjson不是最好的选择,因为它被转换成ajax后,直接尝试jquery。
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
}
}
});发布于 2022-03-17 10:58:30
我使用了Serge的ajax函数(上面标记为答案),并修改了自己的控制器以提供一个字符串,而不是JSON对象,因为活动服务器不断向我的JSON请求发送一个文本响应。
控制器现在看起来如下:
[HttpGet]
[ActionName("GetPriceFromSetAttributes")]
public IActionResult GetPriceFromSetAttributes(int productId, string productAttributeName, string productAttributeValueName)
{
var price = _repository.GetProductPriceFromSetAttributes(productId, productAttributeName, productAttributeValueName).AttributePrice.ToString();
return Content(price);
}它也比以前更快地获取所需的数据。
https://stackoverflow.com/questions/71350978
复制相似问题