1

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?

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
t.mikael.d
  • 5,054
  • 2
  • 16
  • 11

1 Answers1

1

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

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • Thanks for the reply, this was the first thing i tried. And im still getting back the same wierd response and the same content-type in the response header. "Content-Type text/plain; charset=utf-8". – t.mikael.d Nov 25 '11 at 15:25