weixin_33674976 2015-05-07 07:42 采纳率: 0%
浏览 18

Ajax获取/发布相同的方法

I am trying to call a server method from my controller using ajax Get method, in which i have provided data. Method accepts arguments does some work and returning a list. I want to use same method for post.

I am getting undefined error when i try to do this.

My Get method looks like:

$.ajax({
        traditional:true,
        type: "GET",
        url: "/Graphs/chartData",
        data: { id: 0, WeatherAll: 0, paramSites: 0 },
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (r) {
            alert(r);
        },
        failure: function (r) {

            alert(r.d);
        },
        error: function (r) {
            alert(r.d);
        }
});

My Post method has some google charts like this:

google.load("visualization", "1", { packages: ["corechart"] });
    google.setOnLoadCallback(drawChart);
    function drawChart() {
        var options = {
            backgroundColor: 'transparent',
            title: 'Humidity/Temperature Measure',
        };
        var idSite = $('#DDLSites').val();
        var idWeatherALL = $('#DDLParameterWeatherAll').val();
        var idParamSites = $('#DDLParameterWeatherSites').val();
        $.ajax({
            traditional: true,
            type: "POST",
            url: "/Graphs/chartData",
            data: '{}',
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (r) {
                alert(r);
                var data = google.visualization.arrayToDataTable(r);
                var options = {
                    title: 'Humidity/Temperature Measure for Site 1',
                    'backgroundColor': 'transparent',
                    vAxis: {
                        title: 'Temperature/Humidity',
                    },
                    hAxis: {
                        title: 'Time',
                    },
                };
                var chart = new google.visualization.AreaChart($("#chart")[0]);
                chart.draw(data, options);
            },
            failure: function (r) {

                alert(r.d);
            },
            error: function (r) {
                alert(r.d);
            }
        });
    }

My ChartData Method is:

[AcceptVerbs("Get", "Post")]
public JsonResult chartData(int id, int WeatherAll,int  paramSites)
{
    string ids = Convert.ToString(id) + Convert.ToString(WeatherAll) + Convert.ToString(paramSites);
    List<object> chartData = new List<object>();
    chartData.Add(new object[]
    {
        "Date Time", "Temp", "Humidity"
    });
        ////Some Code here
    return Json(chartData);
}

I am unable to figure out how i can do it. Please let me know of any way that is convenient. Thanks

  • 写回答

2条回答 默认 最新

  • ~Onlooker 2015-05-07 08:09
    关注

    Here goes my solution, I created sample Action with AJAX Calls.

    Let Controller Action be -

    [AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
    public JsonResult Data(int id, string s)
    {
        return Json("Test", JsonRequestBehavior.AllowGet);
    }
    

    let the buttons in HTML be -

    <input type="button" value="GET" id="get" />
    <input type="button" value="POST" id="post" />
    

    JQuery code to make the AJAX POST and GET -

    @section scripts{
    <script>
        $(function() {
            $("#get").click(function() {
                $.ajax({
                    traditional: true,
                    type: "GET",
                    url: "/home/data",
                    data: { id: 2, s : 'Test'},
                    success: function (r) {
                        alert(r);
                    },
                    failure: function (r) {
    
                        alert(r.d);
                    },
                    error: function (r) {
                        alert(r.d);
                    }
                });
            });
    
            $("#post").click(function () {
                $.ajax({
                    traditional: true,
                    type: "POST",
                    url: "/home/data",
                    data: JSON.stringify({ id: 4, s: 'Test' }),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function (r) {
                        alert(r);
                    },
                    failure: function (r) {
    
                        alert(r.d);
                    },
                    error: function (r) {
                        alert(r.d);
                    }
                });
            });
        })
    </script>
    }
    

    Make sure proper Route Config entry is present -

     routes.MapRoute(
                    name: "Default",
                    url: "{controller}/{action}/{id}/{s}",
                    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, s = UrlParameter.Optional }
                );
    

    When you make the AJAX GET -

    enter image description here

    When you make the AJAX POST -

    enter image description here

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?