当我尝试从Ajax调用简单方法时出现错误500

时间:2018-09-18 09:50:32

标签: c# asp.net ajax visual-studio

我正在用一个控制器构建一个简单的aspx页面。

这是aspx页面的代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1.Default" %>

<%

%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <script src="https://cdn.zingchart.com/zingchart.min.js"></script>
        <script>
            zingchart.MODULESDIR = "https://cdn.zingchart.com/modules/";
            ZC.LICENSE = ["569d52cefae586f634c54f86dc99e6a9", "ee6b7db5b51705a13dc2339db3edaf6d"];
        </script>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <link href="https://fonts.googleapis.com/css?family=Roboto" rel="stylesheet">

</head>

<body>
    <div id="container">

        <div id="myChart"></div>
    </div>
     <div id="container1">
        <button id="clear">Clear</button>
        <button id="stop">Stop</button>
        <button id="start">Start</button>
      </div>
    <script>
    var myConfig = {
      //chart styling
      type: 'line',
      globals: {
        fontFamily: 'Roboto',
      },
      backgroundColor: '#fff',
      title: {
        backgroundColor: '#1565C0',
        text: 'ECG Real Time',
        color: '#fff',
        height: '30x',
      },
      plotarea: {
        marginTop: '80px'
      },
      crosshairX: {
        lineWidth: 4,
        lineStyle: 'dashed',
        lineColor: '#424242',
        marker: {
          visible: true,
          size: 9
        },
        plotLabel: {
          backgroundColor: '#fff',
          borderColor: '#e3e3e3',
          borderRadius: 5,
          padding: 15,
          fontSize: 15,
          shadow: true,
          shadowAlpha: 0.2,
          shadowBlur: 5,
          shadowDistance: 4,
        },
        scaleLabel: {
          backgroundColor: '#424242',
          padding: 5
        }
      },
      scaleY: {
        guide: {
          visible: false
        },
        values: '0:100:25'
      },
      tooltip: {
        visible: false
      },
      //real-time feed
      refresh: {
        type: 'feed',
        transport: 'js',
        url: 'feed()',
        interval: 500
      },
      plot: {
        shadow: 1,
        shadowColor: '#eee',
        shadowDistance: '10px',
        lineWidth: 5,
        hoverState: {
          visible: false
        },
        marker: {
          visible: false
        },
        aspect: 'spline'
      },
      series: [{
        values: [],
        lineColor: '#2196F3',
        text: 'Blue Line'
      }, {
        values: [],
        lineColor: '#ff9800',
        text: 'Orange Line'
      }]
    };

    zingchart.render({
      id: 'myChart',
      data: myConfig,
      height: '100%',
      width: '100%'
    });

    //real-time feed random math function
    window.feed = function(callback) {

        $.ajax({
        type: "POST",
        url: "Default.aspx/RefreshChartEcg",
        //data: '{name: "' + $("#<%=txtLastID.ClientID%>")[0].value + '" }',
        data: '10',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) {
            var tick = {};
            tick.plot0 = response.d;
            tick.plot1 = parseInt(10 + 90 * Math.random(), 10);
            callback(JSON.stringify(tick));
        },
        failure: function(response) {
            alert(response.d);
        }
    });

      /*$.ajax({
          type: "POST",
          url: "Default.aspx/OnSubmit",
          data: "",
          error: function (XMLHttpRequest, textStatus, errorThrown) {
              //alert("Request: " + XMLHttpRequest.toString() + "\n\nStatus: " + textStatus + "\n\nError: " + errorThrown);
          },
          complete: function (jqXHR, status) {
              //alert("complete: " + status + "\n\nResponse: " + jqXHR.responseText);
              /*var tick = {};
              tick.plot0 = parseInt(10 + 90 * Math.random(), 10);
              tick.plot1 = parseInt(10 + 90 * Math.random(), 10);
              callback(JSON.stringify(tick));
          }
          success: Repaingchart
      });*/

        function OnSuccesss(response) {
          alert(response.d);
      }




    };
    //clear start stop click events
    document.getElementById('clear').addEventListener('click', clearGraph);
    document.getElementById('start').addEventListener('click', startGraph);
    document.getElementById('stop').addEventListener('click', stopGraph);

    function clearGraph() {
      zingchart.exec('myChart', 'clearfeed')
    }

    function startGraph() {
      zingchart.exec('myChart', 'startfeed');
    }

    function stopGraph() {
      zingchart.exec('myChart', 'stopfeed');
    }
  </script>
    <form id="form1" runat="server">
    <div>
        <asp:Label runat="server" id="HelloWorldLabel"></asp:Label>
         <asp:TextBox ID="txtLastID" runat="server"></asp:TextBox>
    </div>
    </form>
</body>
</html>

这是我的页面控制者:

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Services;

namespace WebApplication1
{
    public partial class Default : System.Web.UI.Page
    {
        private OmniacareDiabesityDataProductionEntities db = new OmniacareDiabesityDataProductionEntities();

        protected void Page_Load(object sender, EventArgs e)
        {
            HelloWorldLabel.Text = "Pippo e pluto";
           // string variabile = "pluto";
        }

        [WebMethod]
        public static string RefreshChartEcg(string lastID)
        {
            /**
             * qui devo recuperare il codice per recuperare le informazioni real time dal database
             * */
            int lastIntID=0;
            if (lastID != "")
                lastIntID = Int32.Parse(lastID);

            return "";
        }
    }
}

现在,如果我尝试启动我的简单应用程序,则可以看到此Web浏览器控制台错误:

POST http://localhost:29319/Default.aspx/RefreshChartEcg 500 (Internal Server Error)

但是,如果我尝试在RefreshCartEcg上启动调试器,则调试控制不会在行代码处通过

3 个答案:

答案 0 :(得分:0)

在Ajax调用中,您是否可以尝试以json格式传递数据,如下所示,

数据:{lastID:“ 10”}

答案 1 :(得分:0)

问题似乎出在您已经将Ajax传递给Web方法的数据中。请参阅此link

上已经给出的答案

Ajax方法应具有以下格式:

data: "{ 'lastID': '10' }" 

OR

data: "{ lastID: '10' }"

答案 2 :(得分:0)

可能有两个错误:

首先,在ajax调用中,就像vikscool,Jitendra Rangpariya和Gopal M所说的那样,您应该给一个JSON对象作为数据。

第二,在处理数字之前,您必须打开JSON

public static string RefreshChartEcg(string data)
    {
        /**
         * qui devo recuperare il codice per recuperare le informazioni real time dal database
         * */
        int lastIntID=0;

        if (data != "")
            json = JsonConvert.SerializeObject(data)
            lastIntID = Int32.Parse(json.lastID);

        return "";
    }