【问题标题】:span tag text not change using JQuery ajax使用 JQuery ajax 不会更改跨度标记文本
【发布时间】:2016-05-28 14:08:31
【问题描述】:

我无法将 json 值赋予 span 标签 实际上我正在使用 jquery ajax 函数

 var productId = $("#product_id").text();
    var city = $("#spn-deliveryloccookies").text();

    $.ajax({
        url: '/Product/est_time',
        type: 'GET',
        data: { 'city': city, productid: productId },
        contentType: 'application/json; charset=utf-8',
        success: function (res) {
            //your success code
            alert(res);
            //$("#p_price").html("");

            $("#p_price").text(res.ourPrice);
        },
        error: function () {
            alert("some error");
        }
    });
});

之后它会产生结果 和具有

的网页

我无法将文本值输入到 span 标签中

在我的 ajax 函数 success 块中有 **alert(res); 并且它工作正常它会产生结果**

但是

$("#p_price").text(res.ourPrice);不工作

【问题讨论】:

  • res是一个字符串,使用dataType: "json",会解析成js对象
  • 感谢天才,它现在可以工作了......简单的事情让我很生气。
  • 另外请注意 GET 没有 contentType。没有发送内容正文

标签: javascript jquery html ajax


【解决方案1】:

您需要将 json 字符串解析为 javascript 对象。然后只有您访问它的属性。

  $("#p_price").text(JSON.parse(res).ourPrice);

【讨论】:

  • 这是使用 dataType:JSON 的替代解决方案。它会工作,尝试更新的代码。如果有任何错误请评论。 @SkAsraf
猜你喜欢
  • 1970-01-01
  • 2021-09-08
  • 2016-02-04
  • 1970-01-01
  • 2015-11-04
  • 1970-01-01
  • 1970-01-01
  • 2017-11-09
  • 2019-03-21
相关资源
最近更新 更多