如何更改文件​​从Json的检索数据? [英] How to change file to retrieve data from Json?

查看:155
本文介绍了如何更改文件​​从Json的检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不知道是不是我的问题是把好了,但这里是我的问题:
我设法从ajax.aspx.cs页让我Generator.aspx页面上一些的JSON值。一切工作正常。我试图从ajax.aspx.cs移动code到Generator.aspx.cs页,但不起作用。

Don't know if my question is put well, but here is my problem: I managed to get on my Generator.aspx page some Json values from ajax.aspx.cs page. Everything works fine. I tried to move the code from ajax.aspx.cs to Generator.aspx.cs page but doesn't work.

下面是我的ajax.aspx.cs code:

Here is my ajax.aspx.cs code:

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();
            }
        }


    }
}

然后我打电话的JSON Generator.aspx页面,如:

And then I call the json in Generator.aspx page like:

<%@ 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...

如果我把同样的code从ajax.aspx.cs到Generator.aspx.cs页面,在我的Generator.aspx,而不是网​​址:('ajax.aspx甲基? =')+rnd7我把网​​址:('Generator.aspx甲基=')+rnd7它抛出我吗?我的网页上的法未规定。我在做什么错了?

If I put the same code from ajax.aspx.cs to Generator.aspx.cs page, and in my Generator.aspx, instead of url: ('ajax.aspx?meth=') + "rnd7", I put url: ('Generator.aspx?meth=') + "rnd7", it throws me on my web page "Method was not specified". What am I doing wrong?

推荐答案

如果你想调用jQuery的AJAX,然后有其他好的方法(使用web服务,或通用处理器)做.i.e。
相反,通过查询字符串发送数据,通过JSON发送,列表格式

If you want to call jquery ajax, then there are other good ways to do it .i.e (using Webservice, or generic handler). Instead of sending data via QueryString, send it by JSON, list format

按照以下简单步骤(web服务)

Follow this simple steps (Webservice)


  1. 新增WebService.asmx:右击项目解决方案 - >添加新项 - >选择Webservice.aspx

  2. 现在,在其中创建一个WebMethod。

  3. 通话从jQuery的AJAX调用此的WebMethod。

参考链接:
的http:// codepedia .INFO / jQuery的 - Ajax的JSON-例如-ASP净SQL的数据库/

Reference Link: http://codepedia.info/jquery-ajax-json-example-asp-net-sql-database/

按照以下简单步骤(web服务)

Follow this simple steps (Webservice)


  1. 新增WebService.asmx:右击项目解决方案 - >添加新项 - >选择通用处理器

  2. 现在,写在里面code逻辑

  3. 呼叫jQuery的Ajax和URL作为通用处理器PATH。

参考链接:
<一个href=\"http://$c$cpedia.info/generic-handler-ashx-file-post-send-json-data-parameters-in-asp-net-c-jquery/\" rel=\"nofollow\">http://$c$cpedia.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");
                }

当您收到错误未指定方法,这意味着如果在秋季最后,如果条件

As you are getting error "Method was not specified", this mean if fall in last if condition

这篇关于如何更改文件​​从Json的检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆