2014-11-03 116 views
2

我開始使用Response.StatusDescription把錯誤信息在我的Ajax用戶調用(見下面的代碼)jQuery的AJAX調用 - 響應狀態說明字符集

問題是與編碼。響應消息帶有重音字母的問題(請參閱下面的AJAX調用)。

它在我的電腦上工作正常,但一旦發佈到服務器,我開始遇到這個問題。

我做錯了什麼?

public JsonResult MyAction(int variable) 
{ 
    if (variable > 0) 
    { 
     //Do Something 
    } 
    else 
    { 
     Response.StatusCode = (int)HttpStatusCode.Unused; 
     Response.StatusDescription = "Dados Inválidos"; //notice the 'á' on the message 
    } 
    return Json(true); 
} 

我的Ajax調用:

$.ajax({ 
    type: 'POST', 
    traditional: true, 
    url: '/testUrl/MyAction', 
    dataType: "JSON", 
    data: { 
     variable: myVal 
    } 
}) 
    .done(function() 
    { 
     //Do something 
    }) 
    .fail(function(error, textStatus, errorThrown) 
    { 
     var msg = error.statusText; //The message received is: "Dados InvÂilidos" 
     if (error.status !== 306) //HttpStatusCode.Unused 
      msg = 'Error occurred'; 

     this.dialogShowError(msg); 
    }); 

UPDATE: 這是響應頭,我收到:

enter image description here

這個問題似乎只發生在Ajax請求。我對Regular ActionResult進行了測試,StatusDescription設置正確。

回答

0

嘗試

Response.Charset = "utf-8"; 

我的預感是HTTP響應報頭有錯誤的字符集。

+0

我已經添加了你的建議,但我仍然遇到同樣的問題。看到我的更新與響應頭。 – HobojoeBr 2014-11-03 19:29:22