如何让mvc动作触发jquery ajax转换器

How to make mvc action fire off jquery ajax converter?

本文关键字:jquery ajax 转换器 mvc      更新时间:2023-09-26

我如何使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"),并且仍然保留所有现有的$.ajax调用,这些调用期望json能够在

中工作

我假设您从服务器返回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
 }
});

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