2

I have a rest url http://server1:8080/platform/collections/123-456-789 which returns an HTML file as a byte array.

How can I get the byte array response using Javascript or jQuery running in server2. I tried

jQuery.ajax({
        url: "http://server1:8080/platform/collections/123-456-789",
        type: "GET",
        dataType: "html",
        crossDomain: true,
        username: "abcd",
        password: "abcd",
        async: true,
        success: function(data) {
            alert("1");
            alert(data);
        },
        error: function (xhr, ajaxOptions, thrownError) {
            alert("error"+xhr.responseText);
            alert("error"+thrownError);
        }
});

I don't go into the success method. How can I get the byte array response successfully?

Edit:

Even anyother way to get byte array responce using javascript or jquery is also appreciated

aaviss
  • 401
  • 2
  • 10
  • 29
  • 1
    What is the error that you get? – Samuel Caillerie Nov 28 '12 at 09:51
  • @user1784484 The request might be timing out. Are you sure your server side logic is working? – Asad Saeeduddin Nov 28 '12 at 09:56
  • For a cross domain request, I think you'll have to return JSONp. – slowe Nov 28 '12 at 09:58
  • You can also use Firebug (Network tab) or Chrome developer tools to have a more detailed information about the error... – Samuel Caillerie Nov 28 '12 at 10:06
  • i checked with chrome developer tools. I am getting the following in the console XMLHttpRequest cannot load "http://server1:8080/platform/collections/123-456-789". Origin "http://localhost:7070" is not allowed by Access-Control-Allow-Origin. – aaviss Nov 28 '12 at 10:19
  • When I added responseencoding=JSON to the url and jsonp: "content" it gives 1 warning Resource interpreted as Script but transferred with MIME type text/html: "http://server1:8080/platform/collections/123-456-789?&responseencoding=JSON&content=jQuery1820634079844225198_1354101397849&_=1354101397871". jquery.js:8304 and a error message Uncaught SyntaxError: Unexpected token – aaviss Nov 28 '12 at 11:17
  • Even anyother way to get byte array responce using javascript or jquery is appreciated – aaviss Nov 28 '12 at 11:36

2 Answers2

1

For several servers (with different domains) you need to enable CORS to allow cross-domain ajax requests. It should be possible as both servers are under your control.

On how to receive binary data with jQuery (which is currently not possible), see http://blog.vjeux.com/2011/javascript/jquery-binary-ajax.html

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • Yes Bergi you are right. Thanks for your response. I wrote a webservice in britesnow which reads the byte response and gives out json response. – aaviss Dec 04 '12 at 05:29
  • jQuery and binary is possible. You may want to look at [my answer over here](http://stackoverflow.com/q/14726729/483113). – sjngm Feb 18 '13 at 21:59
0

CORS is the newer way to go, but jsonp may be a bit easier...

With JSONP, you will need to wrap your file on server1 in some sort of script so you can set the content-type header to javascript and then JSONencode the file and write it to the response. This could be done in a couple of lines in PHP or server side javascript; you'll want the script to end up returning a "javascript file" containing something like this:

document.getElementById('content-holder').innerHTML="<html>this is my file</html>";

On the client (your static html page served from server2), you can then just place your content holder:

<div id='content-holder'></div>

and then a script to pull in content from server1:

<script type="text/javascript">
var getXsS = function(url)
{
    var ss = 's' + 'cr' + 'ip' + 't';
    var cst = document.getElementsByTagName(ss)[document.getElementsByTagName(ss).length-1];
    var ts = 1*new Date();
    var e = document.createElement(ss);
    e.async=1;
    var tsstr = '_ts1_='+ts;
    if((''+url).indexOf('?')==-1){tsstr='?'+tsstr;}else{tsstr='&'+tsstr;}
    var url2 = url+tsstr;
    e.src=url2;
    cst.parentNode.insertBefore(e,cst);
};
getXsS('http://server1:8080/platform/collections/123-456-789');
</script>

Note that if server1 is using SSL then server2 must be using SSL also.