JSS Rest API with jQuery

xvinnyx
New Contributor

I'm trying to hit the rest API from a website using jQuery. Since I ran into a cross site scripting issue, I'm using jsonp as the datatype. Although I'm seeing a 200 status getting returned, there seems to be an issue with reading the data. I tried changing the contentType around to several other options, but they don't seem to work.

Does anyone know a better javascript method for this or have any idea on what I'm doing wrong?

Error message

SyntaxError: expected expression, got '< <?xml version="1.0"" etc

Here's the jquery code snippet.

var jamfAPI = 'https://ourjamfurl /JSSResource/computers/match/useridhere;'
function setHeader(xhr) {
xhr.setRequestHeader('username', 'theusername'); xhr.setRequestHeader('password', 'thepassword'); } $.ajax({
url: jamfAPI,
type: "POST",
dataType: "jsonp",
processData: false,
contentType: "application/json",
beforeSend: setHeader,
success: function () {
alert('success');
},
error: function (xhr, ajaxOptions, thrownError) { alert(xhr.status); alert(xhr.responseText); }, });
4 REPLIES 4

mbuckner
Contributor

This may just be a copy/paste issue with your code, but your first line (var jamfAPI) has an opening ' but no closing '.

Emmert
Valued Contributor

I've had problems where it unpredictably returns XML or JSON if you don't explicitly ask for one or the other. It looks like you are though, but it's ignoring you and sending XML anyway, can you change datatype to "xml" instead?

ebauer
New Contributor II

Did you ever get this issue resolved?

I am having the exact same problem - the api only returns xml even though json is explicitly called for in the ajax request.

Curiously, the api does return json when queried in a REST client, just not to an ajax request??

ebauer
New Contributor II

As of 5/22/2016 per jss tech support:

"We do support ajax queries, and that right now there is a bug with headers in the JSS that's causing it to only return XML."

this bug seems to only apply to javascript ajax calls.