2017-05-03 49 views
1

我有作用的結果,它發送的內容一樣呈現在成功阿賈克斯警報HTML下面如何使用asp.net的MVC和jQuery

public ActionResult MyAction() 
    { 
     string mystring = //doing some thing 

     return Content(mystring , "html"); 
    } 

客戶端

$.ajax({ 
      url: "/MyController/MyAction", 
      type: "POST", 
      dataType: "html", 
      data: details, 
      success: function (response) { 
       if (response != "") { 
        alert(response); 
       }     

      } 
     }); 

我現在的字符串或我發送的迴應是(它可以是更多的數字,如1,2,3動態)

"\\n 1: blah blah.\\n 2: blah blah" 

,並在警告其未來作爲

\n 1: blah blah.\n 2: blah blah 

如何使警覺的樣子

1.blah blah. 
2.blah blah. 

不能改變在服務器端的東西,改變只允許在客戶端

+1

的可能的複製[爲什麼在這個JavaScript警告窗口工作不換行?(http://stackoverflow.com/questions/8967722/why-are-new-lines-not-working-in- this-javascript-alert-window) – George

+0

'return Content(mystring,「html」);'應該是'return Content(mystring,「text/plain」);'問題出在服務器端,不發送你發送的新行「\ n」 – George

回答

1

更新您的客戶端代碼。

替換響應。

$.ajax({ 
     url: "/MyController/MyAction", 
     type: "POST", 
     dataType: "html", 
     data: details, 
     success: function (response) { 
     if (response != "") { 
      response = response.replace(/\\n/g, "\n"); 
      alert(response); 
      }     
     }}); 
+0

@tripathy試試這個希望它可以幫助你。它的工作原理是 –

+0

。謝謝 – tripathy

+0

歡迎!享受代碼! –