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

7
  • 1
    What is the error that you get? Commented Nov 28, 2012 at 9:51
  • @user1784484 The request might be timing out. Are you sure your server side logic is working? Commented Nov 28, 2012 at 9:56
  • For a cross domain request, I think you'll have to return JSONp. Commented Nov 28, 2012 at 9:58
  • You can also use Firebug (Network tab) or Chrome developer tools to have a more detailed information about the error... Commented Nov 28, 2012 at 10:06
  • i checked with chrome developer tools. I am getting the following in the console XMLHttpRequest cannot load "server1:8080/platform/collections/123-456-789". Origin "localhost:7070" is not allowed by Access-Control-Allow-Origin. Commented Nov 28, 2012 at 10:19

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
jQuery and binary is possible. You may want to look at my answer over here.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.