如何通过XHR获取JSON参数

时间:2014-07-22 13:55:52

标签: jquery json asp.net-mvc-3

我想要检索"测试"使用XHR的MVC视图中的参数?

 return Json(new
        {
            sEcho = param.sEcho,
            iTotalRecords = totalRows,
            iTotalDisplayRecords = totalRows,
            aaData = jsonArray,
            test = "testing..."
        },
        JsonRequestBehavior.AllowGet);

我的观点

 var oTable = $('#myDataTable').dataTable({
            "sAjaxSource": "Directory/TableAjaxHandler",
            "fnDrawCallback": function (oSettings) {
                $('#panelBody').scrollTop(50);
            },
            "fnInitComplete": function(oSettings, json) {
              alert("test");
            }
        });

如何在剃须刀(MVC3)视图中获取测试参数

2 个答案:

答案 0 :(得分:1)

返回Json ActionResult没有视图,因为Controller的输出是Json

在jQuery(1.8+)中使用最新的AJAX方法,你可以这样做来访问json对象:

request = $.ajax({
    url: "/Controller/Action",
    type: "POST",
    data: serializedData
});

// callback handler that will be called on success
request.done(function (response, textStatus, jqXHR){
    //add your code here...
});

早期版本的jQuery:

$.ajax({
  url : "Controller/Action",
  type: "POST",
  data : formData,
  success: function(data, textStatus, jqXHR)
  {
    //data - response from server
  },
  error: function (jqXHR, textStatus, errorThrown)
  {

  }
});

答案 1 :(得分:0)

调用你的JsonResult动作,如下所示

$.getJSON("url_of_action", function(data) {
 console.log(data);
 alert(data.test);
});