JavaScript JSON output into a or a

Tags:



$.ajax({
    type: 'POST',
    headers: { "cache-control": "no-cache" },
    url: baseDir + 'somepage.php?ajax=true&rand=' + new Date().getTime(),
    async: true,
    cache: false,
    dataType : "script",
    data: 'somedata=' + JSON.stringify(something),
    success: function(jsonData)
    {
        //do something
    },
    error: function(XMLHttpRequest, textStatus, errorThrown)
    {
        //error
    }
});

I have this code which works ok, the response from page somepage.php is this:

somefunction(55, new Array('1', '2', '3'), 0, 0, 0);
$('#someid').val('123');

Which is evaluated as JavaScript and returns it as plain text.

My question is: how do I put the output into a debug <textarea id="something"><textarea> … or a div, to view the output for debugging?

Answer

Use jQuery.text();

error: function(XMLHttpRequest, textStatus, errorThrown)
{
    $('#myDiv').text(textStatus)
}

Cheers!



Source: stackoverflow