Need some help getting pulse to play nice with RESTful JS

Disclaimer: although I have a good deal of experience with various programming and scripting languages, I am diving headfirst into AJAX and therefore have little to no idea what I am doing.

So the general idea I want to pursue is that of a web UI that lives on the company intranet and helps keep track of tasks and assets. A big part of this is integrating with Deadline. In theory, the REST API provides an excellent way of doing so, but in practice I have hit some roadblocks that I could use some help with.

The main issue seems to be that most browsers have a same-origin policy when it comes to sending HTTP traffic cross-site. This complicates things, since as far as I can tell it requires that all interaction be carried out by way of JSONP. To simplify this, I am currently using jQuery to handle requests to Pulse.

When I feed the jQuery.getJSON() or jQuery.ajax() with a text file containing the JSON, it behaves as expected. However, when I try and get it from Pulse directly via JSONP, a parser error is thrown; what comes back is valid JSON, but the callback is never called. It looks to me like Pulse isn’t built to issue JSONP responses (i.e. the JSON data wrapped in a callback function). Is that correct? If so, is there any way to work around it?

Cobbled together a wrapper in Python, to be running constantly on some machine on the network. JSONP requests from the web app go to the wrapper, it queries pulse and returns the JSON wrapped up in the padding. Only handles GET for now, and no handling for exceptions, but maybe this could be a helpful starting point if someone else needs it:

[code]import urllib, urlparse, httplib2, re, time, BaseHTTPServer

HOST_NAME = ‘myHostname’ # REPLACE WITH YOUR HOSTNAME
PORT_NUMBER = 4101 # PORT FOR THE WRAPPER SERVER

pulseAddr = ‘pulse’ # THE HOSTNAME TO DEADLINE PULSE
pulsePort = 4100

def wrapJSON(inString, callbackName):
# Take input JSON, wrap into JSONP function
return callbackName + “(” + inString + “);”

def redirRequest(method, requestPath):
# Redirect request to pulse, return response
newURL = “http://” + pulseAddr + ‘:’ + str(pulsePort) + requestPath
h = httplib2.Http(".cache")
resp, content = h.request(newURL, method)
return content

def getCallbackName(path):
for theStr in path.split(’?’):
if (theStr[0:9] == ‘callback=’):
theStr2 = theStr.split(’&’)[0]
return theStr2[9:]
return ‘’

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_HEAD(s):
s.send_response(200)
s.send_header(“Content-type”, “application/javascript”)
s.end_headers()
def do_GET(s):
“”“Respond to a GET request.”""
s.send_response(200)
s.send_header(“Content-type”, “application/javascript”)
s.end_headers()
s.wfile.write(wrapJSON(redirRequest(s.command,s.path),getCallbackName(s.path)))

if name == ‘main’:
server_class = BaseHTTPServer.HTTPServer
httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler)
print time.asctime(), “Server Starts - %s:%s” % (HOST_NAME, PORT_NUMBER)
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print time.asctime(), “Server Stops - %s:%s” % (HOST_NAME, PORT_NUMBER)[/code]
Of course it would be much neater if Pulse would recognize JSONP requests and respond accordingly :wink:

Thanks for sharing the solution you found!