return Json error from ASP.NET MVC

后端 未结 4 1892
悲&欢浪女
悲&欢浪女 2020-12-13 06:11

I\'m trying to return an error message via Json from ASP.NET MVC controller. I want to display carriage returns on the screen, so the error will look like:

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-13 06:48

    It seems to me that you already made almost everything correct. The results which you have in the xhr.responseText is a JSON string. So you should insert only one additional call of the JSON.parse function

    .ajax({...
        error: function(xhr, textStatus, exceptionThrown) {
            $('#result').html(JSON.parse(xhr.responseText));
        },
    

    then the data like '"Error1.\u003cbr/\u003eErro2.\u003cbr.\u003e"' will be converted to the string 'Error 1.
    Error 2.
    '
    .

    Inside of success event handler the ajax function call JSON.parse for you, but it do this not inside of the error handler. So converting the server response from JSON string you have to do manually.

提交回复
热议问题