!!! All other answers are wrong.
You should be aware of the http protocol standards and follow its rules. That way all developers can communicate without even speaking to each other. When you send a request using AJAX or any other http client, you can use Accept header to indicate which response content type you accept (expect). And then you use Content-Type of the response to identify the data format and deal with it accordingly.
$.ajax({
type: "POST",
url: "/api/connect/user",
headers: {
"Content-Type": "application/json; charset=utf-8" // what we send
"Accept": "application/json, text/plain; charset=utf-8", // what we accept (expect back)
},
data: "{ 'field': 'data to be sent' }",
success: function(response, status, xhr){
var ct = xhr.getResponseHeader("content-type") || "";
if (ct.indexOf("text/plain") > -1) {
// handle text here
} else if (ct.indexOf("application/json") > -1) {
// handle json here
} else {
throw new Error("Content type " + ct " is not expected/supported.");
}
},
error : function(error) {
// handle error here
},
})
Please note that Accept header tells the server what format you accept, so the server can send you the data in the desired format (json, xml, text, html, etc.). Also please be aware that the server could not support some of the content types, that way you should expect 406 and handle it accordingly. I assume you also handle other http statuses properly.
screen1