Marc Rasmussen
Marc Rasmussen

Reputation: 20545

Text from Ajax request displayed incorrectly

I have the following Ajax:

$.ajax({
    type: 'POST',
    url: '/Module/getDescriptionById',
    data: {
        request: 'ajax',
        module_id: id
    },
    success: function (data)
    {
        $('#description_body').html('<p class="text">'+data+'</p>')

    }
});

Where data is: Med udgangspunkt i Kurt Lewins tre overordnede ledelsesformer, autoritær, demokratisk og laissez faire,så skal du nu tage stilling til følgende udsagn og vælge, hvilken ledelsesform som passer til udsagnet.

however when it prints it to the element the text is:

"Med udgangspunkt i Kurt Lewins tre overordnede ledelsesformer, autorit\u00e6r, demokratisk og laissez faire,s\u00e5 skal du nu tage stilling til f\u00f8lgende udsagn og v\u00e6lge, hvilken ledelsesform som passer til udsagnet."

I have added the charset to my script:

<script src="/site/resources/js/views/modules/library/lib.js"  charset="UTF-8"></script>

Upvotes: 0

Views: 40

Answers (1)

Ry-
Ry-

Reputation: 224856

If you’re able to modify the server-side code, make /Module/getDescriptionById (and anything else that responds with JSON) produce a correct Content-Type header of application/json so jQuery knows to decode it. If you can’t do that, provide a hint:

$.ajax({
    type: 'POST',
    url: '/Module/getDescriptionById',
    data: {
        request: 'ajax',
        module_id: id
    },
    dataType: 'json',
    success: function (data) {
        $('#description_body').html('<p class="text">' + data + '</p>');
    }
});

Also, consider:

$('#description_body')
    .empty()
    .append($('<p>', { class: 'text', text: data }));

Upvotes: 2

Related Questions