0

I'm trying to get the content of CSS file from box.com using direct link and attach it to the head tag, but it doesn't work.

$.ajax({                
        url: "https://app.box.com/shared/static/" + file_hash + ".css",                
        data: { client_id : '6guib1cgnk8uk2kdre45rjg7lt2xayegl94' },
        type: "GET",
        async: false,
        crossDomain: true,
        dataType: "text",
        success: function (data) {
          $("<style></style>").appendTo("head").html(data);
        },
        error: function (error) {
            console.log(error);
        }
    }
);

Cuold anyone explain how to make it work?

It returns an error Object:

Object
abort: function ( statusText ) {
always: function () {
complete: function () {
done: function () {
error: function () {
fail: function () {
getAllResponseHeaders: function () {
getResponseHeader: function ( key ) {
overrideMimeType: function ( type ) {
pipe: function ( /* fnDone, fnFail, fnProgress */ ) {
progress: function () {
promise: function ( obj ) {
readyState: 0
responseText: undefined
setRequestHeader: function ( name, value ) {
state: function () {
status: 0
statusCode: function ( map ) {
statusText: "error"
success: function () {
then: function ( /* fnDone, fnFail, fnProgress */ )
Dmitry
  • 809
  • 3
  • 16
  • 29

1 Answers1

2

This is the error I receive when I execute your code:

XMLHttpRequest cannot load https://app.box.com/shared/static/2mbz6a9olevi86oi43kk.css. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://example.com' is therefore not allowed access.

The problem is that you're making a cross-domain (CORS) request, but JavaScript is limited to requesting resources on the same domain unless the target application specifically allows it.

Box does not allow CORS requests by default, but they will enable them on per-application basis. You'll need to contact Box support with a description of your use case.

Community
  • 1
  • 1
John Hoerr
  • 7,955
  • 2
  • 30
  • 40