如何更改文件以从Json检索数据?

时间:2016-01-18 09:40:34

标签: c# asp.net json

不知道我的问题是否妥善,但这是我的问题: 我设法在我的Generator.aspx页面上从ajax.aspx.cs页面获取一些Json值。一切正常。我试图将代码从ajax.aspx.cs移到Generator.aspx.cs页面,但是没有用。

这是我的ajax.aspx.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Diagnostics;


//for JsonConvert. I installed Json.NET. info: http://stackoverflow.com/questions/18784697/how-to-import-jsonconvert-in-c-sharp-application
using Newtonsoft.Json;

namespace PumpsCurrents
{
    public partial class ajax : System.Web.UI.Page
    {

        Random random1 = new Random();
        double RandomDouble(double min, double max)
        {
            return (max - min) * random1.NextDouble() + min;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            Page.Controls.Clear();
            Random rand = new Random();

            int CPUloading = rand.Next(0, 100);
            int CPUcore = CPUloading - 30 < 0 ? 0 : rand.Next(0, CPUloading - 30);
            int Disk = rand.Next(56, 1024);

            Response.Write("{\"cpu\":" + CPUloading + ", \"core\":" + CPUcore + ", \"disk\":" + Disk + "}");

            this.Response.Clear();
            this.Response.ClearHeaders();
            try
            {
                var method = this.Page.RouteData.Values["meth"] as string;
                if (string.IsNullOrEmpty(method))
                {
                    method = this.Page.Request.QueryString["meth"];
                }
                if (string.IsNullOrEmpty(method))
                {
                    method = this.Page.Request.Form["meth"];
                }
                if (string.IsNullOrEmpty(method))
                {
                    throw new Exception("Method was not specified");
                }


                //START Random Value for Generator:


                if (method == "rnd7")
                {

                   this.Page.Response.ContentType = "application/json7";                    

                    //Functia RandomDouble(min,max) e declarata la inceputul programului 
                    //random pt curenti
                   double nr7 = RandomDouble(1,100); // creates a number between 1 and 99   
                   double nr8 = RandomDouble(1, 100); // creates a number between 1 and 99
                   double nr9 = RandomDouble(1, 100); // creates a number between 1 and 99
                   double nr10 = RandomDouble(1, 100);  // creates a number between 1 and 99

                    //random pt tensiune
                   double nr11 = RandomDouble(1, 100); // creates a number between 1 and 99
                   double nr12 = RandomDouble(1, 100); // creates a number between 1 and 99
                   double nr13 = RandomDouble(1, 100); // creates a number between 1 and 99
                   double nr14 = RandomDouble(1, 100); // creates a number between 1 and 99
                   double nr15 = RandomDouble(1, 100); // creates a number between 1 and 99

                    //random pt presiune
                   double nr16 = RandomDouble(1, 100); // creates a number between 1 and 99
                   double nr17 = RandomDouble(1, 100); // creates a number between 1 and 99
                   double nr18 = RandomDouble(1, 100); // creates a number between 1 and 99
                   double nr19 = RandomDouble(1, 100); // creates a number between 1 and 99
                   double nr20 = RandomDouble(1, 100); // creates a number between 1 and 99
                   double nr21 = RandomDouble(0, 1); // creates a number between 1 and 99

                   var str7 = "{\"val7\":" + nr7.ToString() + ",\"val8\":" + nr8.ToString() + ",\"val9\":" + nr9.ToString() + ",\"val10\":" + nr10.ToString() + ",\"val11\":" + nr11.ToString() +
                        ",\"val12\":" + nr12.ToString() + ",\"val13\":" + nr13.ToString() + ",\"val14\":" + nr14.ToString() + ",\"val15\":" + nr15.ToString() +
                        ",\"val16\":" + nr16.ToString() + ",\"val17\":" + nr17.ToString() + ",\"val18\":" + nr18.ToString() + ",\"val19\":" + nr19.ToString() + ",\"val20\":" + nr20.ToString() + ",\"val21\":" + nr21.ToString() +
         "}";
                    var json7 = new JavaScriptSerializer().Serialize(str7);
                    this.Page.Response.Write(json7);
                }

                //END Random Value for Generator



                this.Response.Flush();
                if (this.Response.SuppressContent)
                {
                    this.Response.Close();
                }
            }
            catch (Exception ex)
            {
                //if (ex is AuthenticationException)
                //{
                //    this.State.SignOut();
                //}
                this.Response.Clear();
                this.Response.ClearHeaders();
                this.Response.TrySkipIisCustomErrors = true;
                this.Response.StatusCode = 500;
                this.Response.ContentType = "text/plain";
                this.Response.Write(ex.Message);
                this.Response.End();
            }
        }


    }
}

然后我在Generator.aspx页面中调用json,如:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Generator.aspx.cs" Inherits="PumpsCurrents.Generator" %>
//...
 function test1() {
            $.ajax({
                type: 'GET',
                url: ('ajax.aspx?meth=') + "rnd7",
                contentType: 'application/json7; charset=utf-8',
                dataType: 'json',
                success: function (data7, textStatus7, jqXHR7) {

                   // Bring data from C# using JSON  

                    //current
                    var obj7 = jQuery.parseJSON(data7);
                    var obj8 = jQuery.parseJSON(data7);
                    var obj9 = jQuery.parseJSON(data7);
                    var obj10 = jQuery.parseJSON(data7);

                    //voltage
                    var obj11 = jQuery.parseJSON(data7);
                    var obj12 = jQuery.parseJSON(data7);
                    var obj13 = jQuery.parseJSON(data7);
                    var obj14 = jQuery.parseJSON(data7);
                    var obj15 = jQuery.parseJSON(data7);

                    //pressure
                    var obj16 = jQuery.parseJSON(data7);
                    var obj17 = jQuery.parseJSON(data7);
                    var obj18 = jQuery.parseJSON(data7);
                    var obj19 = jQuery.parseJSON(data7);
                    var obj20 = jQuery.parseJSON(data7);
                    var obj21 = jQuery.parseJSON(data7);

                    //current
                    $('#nr7').html(obj7.val7);
                    $('#nr8').html(obj8.val8);
                    $('#nr9').html(obj9.val9);
                    $('#nr10').html(obj10.val10);

                    //voltage
                    $('#nr11').html(obj11.val11);
                    $('#nr12').html(obj12.val12);
                    $('#nr13').html(obj13.val13);
                    $('#nr14').html(obj14.val14);
                    $('#nr15').html(obj15.val15);

                    //pressure
                    $('#nr16').html(obj16.val16);
                    $('#nr17').html(obj17.val17);
                    $('#nr18').html(obj18.val18);
                    $('#nr19').html(obj19.val19);
                    $('#nr20').html(obj20.val20);
                    $('#nr21').html(obj21.val21);

                    //current
                    t7 = obj7.val7;
                    t8 = obj8.val8;
                    t9 = obj9.val9;
                    t10 = obj10.val10; 

                    //voltage
                    t11 = obj11.val11;
                    t12 = obj12.val12;
                    t13 = obj13.val13;
                    t14 = obj14.val14;
                    t15 = obj15.val15;

                    //pressure
                    t16 = obj16.val16;
                    t17 = obj17.val17;
                    t18 = obj18.val18;
                    t19 = obj19.val19;
                    t20 = obj20.val20;
                    t21 = obj21.val21;


                },
                error: function (jqXHR7, textStatus7, errorThrown) {
                    window.alert(errorThrown);
                }
            });

        }

        setInterval(test1, 3000);
//+other methods...

如果我将ajax.aspx.cs中的相同代码放到Generator.aspx.cs页面和我的Generator.aspx中,而不是url: ('ajax.aspx?meth=') + "rnd7",我放url: ('Generator.aspx?meth=') + "rnd7",它会把我放到我的网站上页面&#34;方法未指定&#34;。我做错了什么?

1 个答案:

答案 0 :(得分:0)

如果你想调用jquery ajax,那么还有其他好办法.i.e(使用Webservice或通用处理程序)。 而不是通过QueryString发送数据,而是通过JSON,列表格式

发送

按照这个简单的步骤(Webservice)

  1. 添加新的WebService.asmx:右键单击项目解决方案 - &gt;添加新项目 - &gt;选择Webservice.aspx
  2. 现在在其中创建一个webmethod。
  3. 从jquery ajax call中调用此web方法。
  4. 参考链接: http://codepedia.info/jquery-ajax-json-example-asp-net-sql-database/

    按照这个简单的步骤(Webservice)

    1. 添加新的WebService.asmx:右键单击项目解决方案 - &gt;添加新项目 - &gt;选择Generic Handler
    2. 现在在其中编写代码逻辑
    3. 使用URL作为Generic Handler PATH调用jquery ajax。
    4. 参考链接: http://codepedia.info/generic-handler-ashx-file-post-send-json-data-parameters-in-asp-net-c-jquery/

      而不是

      var method = this.Page.RouteData.Values["meth"] as string;
                      if (string.IsNullOrEmpty(method))
                      {
                          method = this.Page.Request.QueryString["meth"];
                      }
                      if (string.IsNullOrEmpty(method))
                      {
                          method = this.Page.Request.Form["meth"];
                      }
                      if (string.IsNullOrEmpty(method))
                      {
                          throw new Exception("Method was not specified");
                      }
      

      应该是

      var method = this.Page.RouteData.Values["meth"] as string;
                      if (string.IsNullOrEmpty(method))
                      {
                          method = this.Page.Request.QueryString["meth"];
                      }
                     else if (string.IsNullOrEmpty(method))
                      {
                          method = this.Page.Request.Form["meth"];
                      }
                    else  if (string.IsNullOrEmpty(method))
                      {
                          throw new Exception("Method was not specified");
                      }
      

      当您收到错误“未指定方法”时,这意味着如果条件

      ,则会落入最后