【问题标题】:Looping JSON Success data fetched from webmethod循环从 webmethod 获取的 JSON 成功数据
【发布时间】:2013-01-26 04:07:18
【问题描述】:

我正在尝试使用 ajax 调用来调用 web 方法,如图所示:

<script type="text/javascript">
$(document).ready(function () {
   $("#getdetails").click(function () {
       $.ajax({
          type: "POST",
          url: "Default.aspx/Gettext",
          data: JSON.stringify({SampleText: $('#sampletext').val(), FontType: $('#fonttype').val() }),
          contentType: "application/json; charset=utf-8",
          dataType: "json",
          success: function (response) {
          $("#Result").html(response.d);
          }//success
      });//ajax call
   });//button click

  $("#FontLists").change(function () {
  $('#fonttype').val($('#FontLists option:selected').text());
  });
});//document

网络方法:

<WebMethod()> _
<ScriptMethod(ResponseFormat:=ResponseFormat.Json)>
Public Shared Function Gettext(ByVal SampleText As String, ByVal FontType As String) As String
    Dim List1 As New ArrayList()
    List1.Add(SampleText)
    List1.Add(FontType)
    For Each Str As String In List1
        Return Str
    Next
End Function

现在的问题是,当我单击按钮时,响应只显示文本而不是选定的字体。但是当我在 List1 放置断点时,它显示如下:

【问题讨论】:

  • 你在#Result收到了什么?
  • 只有在我的文本框中输入的示例文本数据,即..在上面的屏幕截图中,#Result 给我的输出是“dfhg”。
  • 为什么要字符串化:JSON.stringify?

标签: javascript ajax vb.net arraylist webmethod


【解决方案1】:

我已经找到了一种方法:

  success: function (response) {
           var result1 = (response.d.SampleText), result2 =(response.d.FontType),
           result = "SampleText: " + result1 + " FontType: " + result2;
           $("#Result").html(result);
  }

在此之前我已经按照way实现了

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-18
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 2017-05-10
    相关资源
    最近更新 更多