Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 您能在同一个ajax调用中同时处理json和html数据类型吗?_Javascript_.net_Jquery_Asp.net_Asp.net Mvc - Fatal编程技术网

Javascript 您能在同一个ajax调用中同时处理json和html数据类型吗?

Javascript 您能在同一个ajax调用中同时处理json和html数据类型吗?,javascript,.net,jquery,asp.net,asp.net-mvc,Javascript,.net,Jquery,Asp.net,Asp.net Mvc,发布jquery ajax时,我是否可以处理json和html返回类型: 例如,这个ajax调用需要返回html $.ajax({ type: "POST", url: url data: data, dataType: "html", success: function (response) { var $html = "<li clas

发布jquery ajax时,我是否可以处理json和html返回类型:

例如,这个ajax调用需要返回html

$.ajax({
            type: "POST",
            url: url
            data: data,
            dataType: "html",
            success: function (response) {
                var $html = "<li class='list-item'>" + response + "</li>";
                $('#a').prepend($html);                           
            },
            error: function (xhr, status, error) {
                alert(xhr.statusText);
            }
        });
$.ajax({
类型:“POST”,
url:url
数据:数据,
数据类型:“html”,
成功:功能(响应){
var$html=“
  • ”+响应+”
  • ”; $('#a')。前置($html); }, 错误:函数(xhr、状态、错误){ 警报(xhr.statusText); } });
    但我想修改它,以便在出现模型错误时可以返回json对象。所以我可以这样做:

    success: function (response) {
              if (response.Error){
                     alert(response.Message);   
              } else {
                  var $html = "<li class='list-item'>" + response + "</li>";
                  $('#a').prepend($html);                           
              }
    
    成功:功能(响应){
    if(response.Error){
    警报(response.Message);
    }否则{
    var$html=“
  • ”+响应+”
  • ”; $('#a')。前置($html); }

    这可能吗?

    对于第一种情况,只需将html放在
    响应.Message
    字段中。返回json对象中包装的html以便于添加状态代码并不少见。

    对于第一种情况,只需将html放在
    响应.Message
    字段中。返回json对象中包装的html并不少见添加一个json对象,以便轻松添加状态代码