2017-07-24 26 views
0

我想解析json響應。 JSON響應在responseText屬性 首先如下所有我得到的JSON響應的未來:modelErrors來自json響應的屬性是undefined

{"Success":false,"Error":true,"ErrorType":1,"ModelErrors":{"Name":"\u0027Name\u0027 must not be empty.","Owner":"\u0027Owner\u0027 must not be empty.","Email":"\u0027Email\u0027 must not be empty.","Password":"\u0027Password\u0027 must not be empty.","Size":"Please provide a valid Number"}} 

當我這樣做console.log(response.responseText)我得到控制檯上面的輸出。

現在我正在捕捉這個函數中的這個響應。一切工作都很成功,但是當錯誤塊執行錯誤時,我得到的ModelErrors屬性未定義。這裏是我的功能

$("form").on('submit', function() { 
     var form = $('.form'); 


     var url = form.attr('action'); 
     var formData = form.serialize(); 
     $.ajax({ 
      type: 'POST', 
      url: url, 
      dataType: 'json', 
      data: formData, 
      success: function (response) { 


       if (response.Success==true) { 
        console.log(response.Success); 

        showMsg(response.Message); 
        //alert(response.Message); 
       } 

       //alert(data); 
      }, 
      error: function (response) { 

       console.log(response.responseText); 

       if (response.responseText.Error === true) 

       { 

        var modelErrors = response.responseText.ModelErrors; 

        console.log(modelErrors); 
        console.log("Check "+modelErrors); 

       } 

      }, 

      cache: false 
     }); 
    }); 

我試過不同的解決方案,但我無法弄清楚我在做錯的地方。在這方面請幫助我。由於

+0

可能是在'\ u0027Name \ u0027'問題。從'modelerrors'中提取'\ u0027'並重試。 –

回答

0

您可以解析JSON

var json='{"Success":false,"Error":true,"ErrorType":1,"ModelErrors":{"Name":"\u0027Name\u0027 must not be empty.","Owner":"\u0027Owner\u0027 must not be empty.","Email":"\u0027Email\u0027 must not be empty.","Password":"\u0027Password\u0027 must not be empty.","Size":"Please provide a valid Number"}}'; 
 
var tmp=JSON.parse(json); 
 
console.log(tmp.ModelErrors);