c# - ajax - How to receive data both html and json from server? -


i have referenced this topic, doesn't solve problem.

i'm using mvc 5 c#. have used code receive data server data type json.

$.ajax({    url: "/mycontroller/myaction",    type: "post",    datatype: "json",    success: function (data) {       if (data.result) {          alert('successfull');       }       else {          alert(data.ex);       }    } }); 

and controller code:

[httppost] public actionresult myaction() {    try    {       return json(new { result = "true", ex = "" });    }    catch (exception e)    {       return json(new { result = "false", ex = e.message });    } } 

i use way data type html:

$.ajax({    url: "/mycontroller/myaction",    type: "post",    datatype: "html",    success: function (data) {       $(".mydiv").append(data);    } }); 

and controller should be:

[httppost] public actionresult myaction() {    return partialview("_mypartialview"); } 

my question is: there way combine of them one?

something this:

$.ajax({    url: "/mycontroller/myaction",    type: "post",    datatype: "json" or "html",    success: function (data) {       if (data.result) {          $(".mydiv").append(data);       }       else {          alert(data.ex);       }    } }); 

and imagination controller code:

[httppost] public actionresult myaction() {    try    {       return partialview("_mypartialview");    }    catch (exception e)    {       return json(new { result = "false", ex = e.message });    } } 

try this:

$.ajax({    url: "mycontroller/myaction",    type: "post",    datatype: "json" or "html",    success: function (data) {       if (data.result) {          $(".mydiv").append(data);       }},    error: function(r,status,exception) {       else {          //handle me       }    } }); 

and in controller:

public actionresult myaction() {    try    {       return partialview("_mypartialview");    }    catch (exception e)    {       response.statuscode = 400;// or error code       return json(new { result = "false", ex = e.message });    } } 

Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -