【问题标题】:How to make mvc action fire off jquery ajax converter?如何使 mvc 动作触发 jquery ajax 转换器?
【发布时间】:2012-02-17 04:29:37
【问题描述】:

如何使 mvc 控制器动作命中 ajax 转换器。我可以修改响应来触发它吗

$.ajaxSetup({
    converters: {
        "mytype1 mytype2": function (result) {
            //do stuff
            return newresult;
        }
    }
});

我曾尝试像这样更改 JsonResult 的内容类型和内容编码:

result.ContentType = "mytype1";
..
response.AppendHeader("Content-Encoding", "mytype1");

但没办法……

编辑:

我真正想要的是更改我的 mvc 操作的返回类型并添加一个 ajax 转换器(“mytype1 json”),并且仍然拥有所有期望 json 工作的现有 $.ajax 调用

【问题讨论】:

  • 你从服务器 xml 返回什么? json?
  • 到目前为止我只尝试过JsonResult..

标签: javascript jquery ajax model-view-controller converter


【解决方案1】:

我假设您从服务器返回 json,您可以在转换器中提取 json 并返回您自己的类型

$.ajaxSetup({
    converters: {
        "json mytype2": function (result) {
            //do stuff            
            return newresult;
        }
    }
});

那么你就可以进行正常的ajax调用了

$.ajax({
 url:'',
 type:'',
 dataType:'json mytype2',
 success:function(data){
  // data will be newresult
 }
});

您不必从服务器端显式调用转换器

【讨论】:

  • 是的。我注意到了这一点。我希望明确调用转换器。我将编辑我的问题以反映我真正想要的内容。
猜你喜欢
  • 1970-01-01
  • 2021-06-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-07
相关资源
最近更新 更多