简体   繁体   中英

Calling RESTFUL service from JQuery

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM