2

I'm trying to read some source code (C language) from my GitHub repository to be shown as text in my webpage. I can access the code in raw mode through https://raw.github.com.

I'm using jQuery GET function to read the data but it doesn't work. Problem is related with XMLHttpRequest and Access-Control-Allow-Origin but, despite I found some related question on stackoverflow (XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin) it didn't work for me. I tried everything.

My jQuery code:

<script type="text/javascript">
  $(document).ready(function() {
    var url = 'https://raw.github.com/raysan5/raylib/master/examples/ex01_basic_window.c';

    $.get(url, function(data) { 
        $('#code').text(data);
      }, 'text');
    });
</script>

Please, could someone help me with this issue? Many thanks!

Community
  • 1
  • 1
Ray
  • 123
  • 2
  • 9

1 Answers1

6

You could try and delete the dot between raw and github:

https://rawgithub.com/raysan5/raylib/master/examples/ex01_basic_window.c

See rawgithub.com, also explained in this blog post:

GitHub discourages this, since they want repo owners to use Github Pages to host specific versions of their files. They discourage it by serving files from the “raw” domain with Content-Type: text/plain instead of Content-type:application/javascript.

This wasn’t a problem until recently, when Google Chrome implemented a security fix that prevents JavaScript from being executed if it has an incorrect Content-type.
This makes sense when you’re viewing pages outside of your control. But when it’s you who’s deciding what scripts to include, it’s a hassle.

As Rob W comments below:

It's worth mentioning that the only reason that this web service solves the OP's problem is that it includes the Access-Control-Allow-Origin: * response header.

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • Nice. It's worth mentioning that the only reason that this web service solves the OP's problem is that it includes the `Access-Control-Allow-Origin: *` response header. – Rob W Nov 23 '13 at 11:40
  • @RobW Good point. I have included it in the answer for more visibility. – VonC Nov 23 '13 at 11:47
  • @RobW I also tried adding in my webpage header but I imagine it must be in the server side not in my webpage... – Ray Nov 23 '13 at 11:53