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
  • possible duplicate of stackoverflow.com/questions/3047311/passing-html-using-json Commented Mar 24, 2013 at 15:38
  • is your error about data format when it is posted or does the post fail to reach your action method? Commented Mar 24, 2013 at 15:39
  • @Dave A I have got error when this text is going to method Commented Mar 24, 2013 at 15:42

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"
        });
Sign up to request clarification or add additional context in comments.

6 Comments

is it possible convert this using JQuery?
@Rafael-JuniorMVCDeveloper, that IS JQuery. Interesting it didn't work. same error?
all the time this same problem. I tried convert also using JQuery but doesn`t help look into my edited post.
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 work
@Rafael-JuniorMVCDeveloper, I'm surprised and maybe reaching, but lets try using ajaxSetup. check my revised answer.
|
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.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.