如何获取JSON字符串并使用AJAX显示

时间:2017-10-25 05:09:47

标签: javascript json ajax vb.net

我的webservice返回JSON字符串,我正在尝试访问MASTERPage.master中的JSOn字符串我该怎么办? “GetJson()”方法返回JSOn字符串。我试图显示该JSON字符串但其失败。 MasterPage.MAster中有问题吗?我们可以在主页面中调用ajax吗?

Webservice方法:

    <WebMethod()> _
Public Function MasterMenu() As String

    ' Dim dt As New DataTable("MenuMaster")

    Dim dt As New DataTable()
    Dim Query As String = "SELECT ID, MENU_NAME FROM SMT_MENU_MASTER order by menu_name"
    Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As New SqlConnection(constr)
        Using cmd As New SqlCommand(Query)
            Using sda As New SqlDataAdapter()
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                sda.SelectCommand = cmd
                sda.Fill(dt)
            End Using
        End Using

        Dim sData As String = GetJson(dt)

        Return sData





    End Using



End Function

在Masterpage.master中的Ajax方法

  $(document).ready(function () {
              $.ajax({

                  type: "POST",
                  url: 'Menu.asmx/MasterMenu',
                  data: "{}",
                  contentType: "application/json; charset=utf-8",
                  dataType: "json",
                  success: function (response) {
                      alert("i am in");
                      alert(response.d);
                  }, error: function (response) {
                      alert("Failed to load data");

                  }

              });
          });

3 个答案:

答案 0 :(得分:0)

          $(document).ready(function () {
          $.ajax({

              type: "POST",
              url: 'Menu.asmx/MasterMenu',
              data: "{}",
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              success: function (data) {
                  alert("i am in");
                  alert(data.sData);
              }, error: function (response) {
                  alert("Failed to load data");

              }

          });
      });

用于查看完整的json及其结构。console.log(data);

答案 1 :(得分:0)

您的网络方法应为共享

Public Class Form2
    Private Sub PenilaianBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
        Me.Validate()
        Me.PenilaianBindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.MahasiswaDataSet)

    End Sub

    Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MahasiswaDataSet.penilaian' table. You can move, or remove it, as needed.
        Me.PenilaianTableAdapter.Fill(Me.MahasiswaDataSet.penilaian)
        NimTextBox.Text = ""
        NamaLabel1.Text = ""
        NimLabel1.Text = ""
        TugasLabel1.Text = ""
        UlanganLabel1.Text = ""
        UtsLabel1.Text = ""
        UasLabel1.Text = ""
        HasilLabel1.Text = ""
        SkorLabel1.Text = ""
    End Sub

    Private Sub btnoke_Click(sender As Object, e As EventArgs) Handles btnoke.Click
        Try
            Dim dt = PenilaianTableAdapter.GetDataBycaridata(NimTextBox.Text)
            If dt.Rows.Count <> 0 Then
                NamaLabel1.Text = dt.Rows(0).Item(1)
                NimLabel1.Text = dt.Rows(0).Item(2)
                TugasLabel1.Text = dt.Rows(0).Item(3)
                UlanganLabel1.Text = dt.Rows(0).Item(4)
                UtsLabel1.Text = dt.Rows(0).Item(5)
                UasLabel1.Text = dt.Rows(0).Item(6)
                HasilLabel1.Text = dt.Rows(0).Item(7)
                SkorLabel1.Text = dt.Rows(0).Item(8)
            Else
                MsgBox("NIM Yang Anda Input Salah Atau Data Anda Belum Diinput. Silahkan Coba Lagi", MsgBoxStyle.Critical, "Info")
                NimTextBox.Text = ""
                NamaLabel1.Text = ""
                NimLabel1.Text = ""
                TugasLabel1.Text = ""
                UlanganLabel1.Text = ""
                UtsLabel1.Text = ""
                UasLabel1.Text = ""
                HasilLabel1.Text = ""
                SkorLabel1.Text = ""
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.Hide()
        Form3.Show()
    End Sub
End Class

答案 2 :(得分:0)

我添加的Web服务存在问题

  <WebService(Namespace:="http://tempuri.org/")> _
  <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
  <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
  <System.Web.Script.Services.ScriptService()> _

并且还导入参考DLL。现在问题解决了我可以使用Ajax调用web服务并在母版页上检索数据

DLL:

Imports Newtonsoft.Json