I'm having a brain fart.
I have a 3rd party server, that has a URL that outputs only a string, eg "ABC123"
This is an indication of which server on a web farm the request is hitting. In my site, I serve a flash app which is hosted by the 3rd party on the web farm.
I have a contact form on my website, I need to get an indication of which server the user is hitting on that web farm. I plan to do this by calling the URL and injecting the result into the contact form, so that it can be added to the message I am sent.
Obviously this is probably not the best method to find out but I am reduced to drastic measures!
I have the following jquery call:
$.ajax({
url:"https://domain.com/folder/filename.asp",
dataType: 'JSONP',
success:function(json){
alert("Success >" + json + '<');
},
error:function(){
alert("POES!!");
}
If I visit the URL in a browser I get the string. However, the ajax call results in a 200 OK response - just no content. What I see is an alert box stating "Success ><"
How can I get the string?
To get JSONP to work, on the remote server you need to send more than just the text -- it works by executing a named callback function so you need something like
name_of_function('ABC123');
where 'name_of_function' will be the name of the callback as supplied by jQuery in the &callback= parameter of the JSONP request URL.
Bugger. So I am fucked then. I can't get anything from the 3rd party server except that text (or the IP, but that is shared for all the servers on the farm)
Off the top of my head I can't think of a way around this, sorry.
TBH I'm not sure it'll work anyway -- is there some clever load-balancing going on that will ensure that requests from a single user always hit the same server? If not then there's no guarantee that you'll be hitting the same server for the Flash as for the JSON.
Try one of these
$.getJSON("https://domain.com/folder/filename.asp&callback=?", function(data) { alert("Success >" + data + '<'); });
note the &callback=? appended to the url
or
$.ajax({ url:"https://domain.com/folder/filename.asp", dataType: 'JSONP', jsonp: false, jsonpCallback: processResponse )}function processResponse(data) { alert("Success >" + data + '<'); }
The first creates a js error message similar to "ABC123 is not defined" - when I view the generated source I can see that the call is injecting
<script src="https://domain.com/folder/filename.asp?callback=jsonp1317979150312"></script>
Which is a step forward, for sure.
The second again gets a 200 OK response but no data.
is there some clever load-balancing going on
I have no idea if it is clever or not, in fact I highly doubt it... but that is the only possible way I can get a hint at which server is serving the content. It is very very hard getting the 3rd party to do anything useful, and I have a sporadic bug in the flash which I am certain is due to one server in the farm serving a broken .swf.
Gosh.. now can I get an error - I am attempting to hack the onerror to get the actual message!
window.onerror = function(errorMsg, url, lineNumber) {
alert("Error caught - " + errorMsg + " : " + url + " " + lineNumber);
return true;
};
This is not the programming I was taught!
Right now, alas, not getting very far, but it seems promising.
Hang on, so you're not actually retrieving a properly formed JSON object?
Change the dataType line to
dataType: 'jsonp text',
That should then treat the returned object as text - you may still need to ensure that the returned object is correctly formed JSON but give that a try first.
.. which works in IE but not Firefox.
Luckily most of our users are on IE.
That should then treat the returned object as text
Will try that, it's better than hacking global onerror.
is it your swf that returns the JSON object?
Can you amend it so that it returns a properly formed object
{ "id" : "ABC123"}
and then in the callback function access it via correct notation
alert (data.id)
You will need to set the datatype back to just 'jsonp' not 'jsonp text'
Originally posted by scudsucker
Bugger. So I am fucked then. I can't get anything from the 3rd party server except that text (or the IP, but that is shared for all the servers on the farm)
dataType: 'jsonp text'
silently fails in both IE & Firefox.
Back to hacking the window.onerror...
I would dearly love to get the 3rd party to do some dev work and make the URL spit out JSON instead of text - after all, we pay them shitloads - but they have a minimum 3 month lead time, and I'd need to motivate why I need them to change something that is rarely required.
Try this lib jquery-jsonp apparently it will handle mal-formed JSON
That also does not work - getting the error function each time, with no data. That library looks cool though - I like the "dataFilter" for pre-processing whatever rubbish the server sends.
But - I am being stupid. I realised that there is probably a crossdomain.xml file - and when I checked, there is, so I am going to do it with flash instead.
why not bounce it through a server script on your own domain? or run a cron to localize it so as to be kind to the data provider?
That is the obvious solution - but, if I set up a script to read the value from my server, it will not necessarily be the same as what the user sees, and I need to know which server the user is hitting. I can't be sure my "proxy" script will be directed to the same server in the webfarm.
By the way, the crossdomain policy is not on that farm - I typo'd the url and unfortunately the domain I need to query does not have a crossdomain.xml.
Back to hacking the js window.onerror message, that's the only promising route I can see. Gosh.