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