Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 1

HTML can't find ashx via ajax when open (return error 404) but works fine when open by Visual Studio

I'm making a new website that came with google pie chart, when I open the html in visual studio everything seems to work just fine, but when I try to open the html outside of visual studio my chart seems to unable to recieve data from ashx, my project right now has one html one ashx and one js file, here's the ashx code:

 

public class game2019 : IHttpHandler {
    
    
    
    public class Data
    {
        public string type { get; set; } 
        public string name { get; set; } 
        public string ratio { get; set; } 
    }
    
    
    
    public void ProcessRequest (HttpContext context) {
        
        string json = string.Empty;

        using (var reader = new System.IO.StreamReader(context.Request.InputStream))
        {
            json = reader.ReadToEnd();
        }

        context.Response.ContentType = "application/json";
        context.Response.Charset = "utf-8";


        
        List<Data> list = new List<Data>();

        MySqlConnection connect = MSC("MariaDBString");



        string s = "SELECT * FROM lastestquotes NoLock";

        MySqlCommand cmd = new MySqlCommand(s, connect);
        MySqlDataAdapter MDA = new MySqlDataAdapter(cmd);
        //System.Data.DataSet DS = new System.Data.DataSet();
        try
        {
            cmd.CommandTimeout = 60;
            DataTable dt = MySQLExecuteReader(connect, DataMode.DataTable, ref cmd, s) as DataTable;

            foreach (DataRow dr in dt.Rows)
            {
                list.Add(new Data { type = dr["type"].ToString(), name = dr["symbolname"].ToString(), ratio = dr["volumeratio"].ToString() });
            }
            context.Response.Write(new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(list));
        }
        catch (Exception ex)
        {
            throw ex;
        }
        
        
        
    }



    public enum DataMode
    {
        SqlDataReader,
        DataTable
    }


    
    public static MySqlConnection MSC(string ConnectString)
    {
        MySqlConnection conn = null;

        try
        {
            conn = new MySqlConnection(WebConfigurationManager.OpenWebConfiguration("~").ConnectionStrings.ConnectionStrings[ConnectString].ConnectionString);
            conn.Open();
        }
        catch (Exception ex)
        {
            if (conn != null && conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
            throw ex;
        }
        finally
        {
            //conn.Close();
            //conn.Dispose();
        }

        return conn;
    }

    public static object MySQLExecuteReader(MySqlConnection conn, DataMode Type, ref MySqlCommand cmd, string sSQLCmd)
    {
        try
        {
            cmd.CommandText = sSQLCmd;
            cmd.Connection = conn;
            if (Type == DataMode.SqlDataReader)
            {
                MySqlDataReader dr = cmd.ExecuteReader();
                return dr;
            }
            else if (Type == DataMode.DataTable)
            {
                DataTable dt = new DataTable();
                using (MySqlDataReader sr = cmd.ExecuteReader())
                {
                    dt.Load(sr);
                }
                return dt;
            }


        }
        catch (Exception Ex)
        {



            throw Ex;

            //return Ex.ToString();

        }
        finally
        {

            cmd = null;
            cmd = new MySqlCommand();

        }
        return null;
    }
    
    
    
    
 
    public bool IsReusable {
        get {
            return false;
        }
    }


}

and here's the js one

 

google.charts.load("current", {packages:["corechart"]});

google.charts.setOnLoadCallback(drawChart);

function drawChart() {

    $.ajax({
        url: 'game2019.ashx',
        type: 'POST',
        contentType: 'application/json; charset=utf-8',
        success: function (result) {

            $(function () {

                var v = new Array();

                v.push(['name', 'ratio']);

                $.each(result, function (i, item) {

                    v.push(new Array(item.name, parseFloat(item.ratio)));

                });

                var data = google.visualization.arrayToDataTable(v);


                var options = {
                    title: 'No Title',
                    pieHole: 0.2,
                    'width': 'auto',
                    'height': '300',
                    textStyle: {
                        color: '#fff',						
                        fontSize: 18,
                        fontName: 'Microsoft JhengHei',		
                        bold: false,
                        italic: false
                    },
                    titleTextStyle: {
                        color: '#fff',						
                        fontSize: 22,
                        fontName: 'Microsoft JhengHei',		
                        bold: false,
                        italic: false
                    }
                };

                var chart = new google.visualization.PieChart(document.getElementById('ten-chart'));
                chart.draw(data, options);

                

            })

        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(jqXHR.status);
            alert(textStatus);
            alert(errorThrown);
        }
    });

Can anyone help?