页面返回HTML而不是JSON

时间:2018-03-19 09:29:51

标签: asp.net json

编译并运行时,页面显示有效的JSON;但是,当使用WebClient调用同一页面时,整个HTML代码都会通过,因此无法解析JSON。

'ASP Code:
        <%@ Import Namespace="System.IO"%>
        <%@ Import Namespace="System.Data"%>
        <%@ Import Namespace="System.Data.OleDb"%>
        <%Response.AppendHeader("Access-Control-Allow-Origin", "*")
        Response.AppendHeader("Content-type", "application/json")%>
        <%
        Dim conn As OleDbConnection
        Dim objAdapter As OleDbDataAdapter
        Dim objTable As DataTable
        Dim objRow As DataRow
        Dim objDataSet As New DataSet()
        Dim outp
        Dim c
            conn = New OleDbConnection("Provider=sqloledb;Data Source=test\SQLEXPRESS;Initial Catalog=TestDb;User ID=sa;Password=test;OLE DB Services=-2;")
            objAdapter = New OleDbDataAdapter("select * from cars", conn)
        objAdapter.Fill(objDataSet, "myTable")
        objTable=objDataSet.Tables("myTable")

        outp = ""
        c = chr(34)
        for each x in objTable.Rows
        if outp <> "" then outp = outp & ","
                outp = outp & "{" & c & "Make" & c & ":" & c & x("make") & c & ","
                outp = outp & c & "Model" & c & ":" & c & x("model") & c & ","
                outp = outp & c & "Year" & c & ":" & c & x("year") & c & "}"
        next

            outp = "{" & c & "records" & c & ":[" & outp & "]}"

            Response.Write(outp)
            conn.Close()
        %>

        //Json Purser:
            private void btnDeserialize_Click(object sender, EventArgs e)
            {
                System.Net.WebClient client = new System.Net.WebClient();
                string json = client.DownloadString("http://localhost:82");// Expecting JSon, Found HTML

                Cars c= (new JavaScriptSerializer()).Deserialize<Cars>(json);
                MessageBox.Show(c.Make);
            }

1 个答案:

答案 0 :(得分:0)

我通过从ASP页面中删除所有HTML标记并仅保留包含ASP代码的表单标记来解决此问题。像魔术一样工作!