0

I am trying to call a resource from a server using REST, the Restful Service is on another server. When i run the script below i get XMLHttpRequest cannot load file:///C:/xampp/htdocs/Rest_Client/application/views/www.creadevz.com/Restful_Server/index.php/api/Example/users/. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

<script>
    document.write('Started');
    jQuery.ajax({            
        type: "GET",           
        url: "www.creadevz.com/Restful_Server/index.php/api/Example/users/",            
        dataType: "json",
        crossDomain: true,
        success: function(html){
            document.write('success');
            document.write(html);
        }        
    }); 
</script>

I have 2 Questions:
1- Why did it append file:///c:/xampp/htdocs/Rest_Client/application/views
2- how to over come the cross domain resource access restrictions.

Extra information:
1- I am using CodeIgniter with Philip Sturgeon's REST server.
2- I followed the Philip Sturgeon's tutorial and used the Example api supplied with the framework
3- I used hurl.it to test the api and it worked fine.

Solutions I have found:
1- CORS
2- JSONP

Most opinions online suggest CORS over JSONP but i am not sure how to implement it in an efficient way, also since hurl.it called the api perfectly there must be a way they are over coming the cross domain resource access restrictions without the CORS Headers.

Thanks in Advance for your help

Edit:
I failed to mention that this is done with the purpose of using it with Phonegap

In an attempt to use the CORS Headers efficiently i added it at the start of the response function in Rest_Controller.php

Ahmed Saad
  • 177
  • 2
  • 12
  • you cannot make cross domain ajax requests – Victor Radu Sep 18 '15 at 12:08
  • @tripleb what are you saying man. – joyBlanks Sep 18 '15 at 12:09
  • 1
    You have to use the full URL http://www.creadevz.com/Restful_Server/index.php/api/Example/users/ observe the missing http:// in the start – Akash Yadav Sep 18 '15 at 12:09
  • Use this flag if in chrome `--allow-file-access-from-files`. **[Read more](http://stackoverflow.com/a/18587027/2065039)** – Guruprasad J Rao Sep 18 '15 at 12:11
  • Use a protocol --> `http://www.creadevz.com/Restful_Server/index.php/api/Example/users/` and be aware for CORS.. btw are you using a server? – GuyT Sep 18 '15 at 12:12
  • 1
    What are you asking? It seems you already know the answer on question 2 why you get the error and know two solutions, only you haven't tried both. So this sin't a good SO-question. http://stackoverflow.com/help/how-to-ask Answer on point 1 is simply add "http://" – Timmetje Sep 18 '15 at 12:12
  • 1
    HTTP header `Access-Control-Allow-Origin` should allow cross domain ajax requests – Raymond Nijland Sep 18 '15 at 12:19
  • @TimDev I asked about how to use it efficiently within this context (Framework) adding the header before every request in every resource isn't exactly optimum, also my search results yielded solutions using plain php not code igniter i would rather use the libraries of codeigniter if they are present. i will read the how-to-ask and hopefully ask better questions next time. – Ahmed Saad Sep 18 '15 at 12:22
  • @GuyT yes i am using a server. – Ahmed Saad Sep 18 '15 at 12:23

2 Answers2

2

To answer you first question please correct is url ,it shoud have "http://" now your code will look like this .

<script>
    document.write('Started');
    jQuery.ajax({            
        type: "GET",           
        url: "http://www.creadevz.com/Restful_Server/index.php/api/Example/users/",            
        dataType: "json",
        crossDomain: true,
        success: function(html){
            document.write('success');
            document.write(html);
        }        
    }); 
</script>

This will now not append append file:///c:/xampp/htdocs/Rest_Client/application/views

Hasya
  • 581
  • 4
  • 3
0

It's simple, if you are working on chrome, you can install this plugin , it works for me. I hope that works for you.

Good luck

user2120121
  • 665
  • 1
  • 6
  • 15