【问题标题】:unable to set result from ajax success to div html in mvc无法将ajax成功的结果设置为mvc中的div html
【发布时间】:2017-08-24 09:48:00
【问题描述】:

我有下面的代码,我正在调用一个动作并在 ajax 成功中获得部分视图。

但我无法将 html 设置为 div-Graph,但我在警报中未定义; alert($('#div-Graph').html());整个 html 正在消失...

对此有任何想法吗?

$(document).ready(function () { $('#ChartTypes').change(function () {

        var selectedID = $(this).val();
        $.ajax({
            url: '/charts/GetChart/4',
            type: 'GET',
            success: function (result) {
                debugger    ;
                $('#div-Graph').html(result.toString());
                alert( $('#div-Graph').html());
            }
        });

    });
});

如果您需要更多代码部分,请告诉我。尝试了很多解决方案:(

谢谢 拉格什

【问题讨论】:

  • 您希望结果为 JSON 格式吗?如果是这样,您可能需要考虑 JSON.stringify(result) 而不是 result.toString()
  • 在 ajax 中你不能像 url: '/charts/GetChart/4', 这样传递参数。您可以在data 中指定参数,如下$.ajax({ url: '/charts/GetChart', data:{paramName: paramValue},type: 'GET',success: function (result)
  • 为什么还要把它转换成字符串?

标签: javascript jquery html ajax asp.net-mvc


【解决方案1】:

您可以像这样直接将其附加到类/ID。您不能在警告框中显示整个 div。

  success: function (result) {
       alert(result);
       var res = JSON.stringify(result);
      $('#div-Graph').append(res);    
  }

【讨论】:

  • 谢谢!但它仍然是未定义的,在调试时;执行后 $('#div-Graph').append(result.toString());整个 html 正在关闭.. :(
  • 立即尝试。我已经更新了我的答案。在此之前首先检查您是否真的使用console.log(result);alert("result"); 获得输出,然后再执行其他任何操作。
  • 谢谢 :) 结果现在已附加,但与预期不符。你回答了这个问题。谢谢
  • 乐于助人:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多