Forums/Community/Zendesk Apps

Ajax returned data/datatypes

Chad Pickerell
posted this on August 09, 2012 15:50

Hi,

I have a few questions regarding the datatypes returned from an ajax request and the manipulation of said data.

My app is requesting information from a web service that returns Json by default but can return Xml if specified in the 'Accept:' header.  It seems that no matter what I set for 'dataType' in my ajax request it always comes back in Xml.  I am not sure if this is because of the browser I am using or if it is the way the Zendesk server sends the request.  I have been using Chrome's built-in javascript console to examine the request/response made by my app and the response is always in Xml, copying and pasting the same request into Fiddler gives me a response in Json.

To look at the data coming back from the request I have done this:

'myAjaxRequest.always' : function(e1, e2, e3) {
  console.log(e1);
  console.log(e2);
  console.log(e3);
}

If I don't specify a dataType in my ajax request then e1 comes back as a Document object containing the data I requested, e2 is the status message, and e3 is the jQuery XmlHttpRequest object.  If I specify the dataType as 'json' then e1 is the jQuery XmlHttpRequest object, where as e2 and e3 are nothing.  Maybe this is a result of how my web service handles the request, I am not sure.

In both scenarios mentioned above I am having a heck of a time manipulating the returned data into a nice object that can be passed to a template.  No matter what I try to do to the object in the console I can't seem to pull out the desired data.  This is probably a result of my inexperience with javascript/jQuery but any suggestions or comments are much appreciated.

Thanks,

Chad

 

Comments

User photo
James Rosen
Zendesk

We're working on improving our proxy support. For now, you can pass the content type as a parameter:

myAjaxRequest: {
url: 'https://example.com/widgets',
data: {
contenttype: 'application/json'
}

I'll add that to the docs for future reference. Eventually we will change the proxy to use the Accept header you provide.

August 13, 2012 19:58
User photo
Maxime
Product Manager

Hi Chad,

We have now released our new proxy which should address most of the issues you were experiencing, to see how to use this one simply follow this link:

http://developer.zendesk.com/documentation/apps/reference/request.h...

Let me know if you have any questions!

Maxime

July 12, 2013 13:51