jQuery Ajax, weird response from .NET server

361 views Asked by At

I'm trying to replace an ajaxpro script with jQuery, but the response i'm getting from the server when using either ajaxpro or jquery is something i don't recognise.

This is the jquery call:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/ajaxpro/CMS.ItemRetrieve.ashx",
    data: jsonData,
    beforeSend: function(xhr) {
        xhr.setRequestHeader("X-AjaxPro-Method", "ItemRetrieve");
        xhr.setRequestHeader("X-Requested-With", "XMLHttpRequest");
    },
    success: function(responseText) {
        console.log(responseText);
    }
})

And this is the weird response:

[0,"\r\n\r\n<div id=\"content\">test</div>\r\n "];/*

I'm expecting HTML or XML in return, but this seem to be an array? I don't understand the escaping and wierd end. I tried setting dataType to json, but it's not json, not html, maybe javascript? Server response content-type seem to be set to text.

So my question is, how do i use this response as HTML, or convert it to HTML?

1

There are 1 answers

1
Rory McCrossan On

Without specifying dataType, jQuery makes a best-guess at the format of the data it receives back from an AJAX call. 90% of the time it's right. The other times it needs a little help.

Try this:

$.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "/ajaxpro/CMS.ItemRetrieve.ashx",
    data: jsonData,
    dataType: "html", // Explicitly set the return data type
    ...
});

More information here