不知道我的問題是否放好,但這是我的問題: 我設法從我的Generator.aspx頁面獲取ajax.aspx.cs頁面的一些Json值。一切正常。我試圖將代碼從ajax.aspx.cs移動到Generator.aspx.cs頁面,但不起作用。如何更改文件以從Json檢索數據?
這裏是我的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...
如果我把相同的代碼從阿賈克斯。 aspx.cs到Generator.aspx.cs頁面,並在我的Generator.aspx中,而不是url: ('ajax.aspx?meth=') + "rnd7",
我把url: ('Generator.aspx?meth=') + "rnd7",
它拋出我的網頁「方法沒有指定」。我究竟做錯了什麼?
我認爲你必須使用另一種方法進行此操作而不是Page_Load() –
你是說我應該重命名Generator.aspx.cs頁面的Page_Load()方法?或者ypu是什麼意思? – Ionut