1

why JSON doesn't work with html text (var text_html = '<p></p><t></t>'; ) but this will be work correct (var text_html = 'example';)

doesn't work

var text_html = JSON.parse('<p></p><t></t>'); 

Problem:

 function Save() { var text_html = '<p></p><t></t>'; $.ajax({ url: '@Url.Action("DodajTematSave", "StronaGlowna")', dataType: "json", data: { My_Text: text_html }, type: "POST", async: false, error: function () { }, success: function (data) { if (data.Success) { alert('success'); } } }); } </script> public JsonResult DodajTematSave(string My_Text) { return Json(new { Success = true}); } 

also this doesn`t work

var dom_string = '<div>xxx<div>yyy</div></div>'; var text_html = dom_string.innerText(); 

also this doesn`t work

<script type="text/javascript"> function Save() { var Temat_controll = $('#Temat').val(); var Streszczenie_controll = $.parseJSON('<p></p><t></t>'); var PelnyOpis_controll = $('#PelnyOpis').text(); $.ajaxSetup({ contentType: "application/json; charset=utf-8", dataType: "json" }); $.ajax({ url: '@Url.Action("DodajTematSave", "StronaGlowna")', dataType: "json", data: { Temat: Temat_controll, Streszczenie: Streszczenie_controll, PelnyOpis: PelnyOpis_controll }, type: "POST", async: false, error: function () { }, success: function (data) { if (data.Success) { alert('success'); } } }); } </script> 
3

3 Answers 3

2

try this:

var Streszczenie_controll = $.parseJSON('<p></p><t></t>'); 

and use ajaxSetup to instruct JQuery how to handle the data type

 $.ajaxSetup({ contentType: "application/json; charset=utf-8", dataType: "json" }); 
6
  • is it possible convert this using JQuery?CommentedMar 24, 2013 at 16:01
  • @Rafael-JuniorMVCDeveloper, that IS JQuery. Interesting it didn't work. same error?CommentedMar 24, 2013 at 16:23
  • all the time this same problem. I tried convert also using JQuery but doesn`t help look into my edited post.CommentedMar 24, 2013 at 16:28
  • When I write this-> var dom_string = "<div>xxx<div>yyy</div></div>"; alert("Variable y value is " + typeof (dom_string)); I get information y is String but doesn`t workCommentedMar 24, 2013 at 16:40
  • @Rafael-JuniorMVCDeveloper, I'm surprised and maybe reaching, but lets try using ajaxSetup. check my revised answer.CommentedMar 24, 2013 at 17:00
2

Because those are escaping characters in JSON. You will have to parse html in a way to make it JSON friendly if you wanted it passed through JSON.

0
    2

    For this people who have problem with this I can show another way to fix this problem but very ugly click here

    0

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.