0

I am hosting an html page on localhost:8888 with a MAMP Server and I am trying to get some data from a JSON file which I am hosting on localhost:3000 on the 'categories' route. Firstly, I wanted to know is this possible ? If it is not possible, is it possible for me to route the JSON data to another site. If it is possible, here is the script I have embedded in my HTML

    <script>
      $(document).ready(function(){
         setInterval(test,500);
         console.log("document ready");
         alert('page ready');
      });
      function test() {
         $.ajax({
           url:"HTTP://localhost:3000/categories",
           dataType: 'jsonp',
           success: function(json){
            $("#Address1").html(json[0]["id"]);;

           }
        });
     }
   </script>

Here is the JSON file :

[{"_id":"5624711f1a530785d511e747","__v":0,"name":"Beverages","description":"Soft drinks, coffees, teas, beers, and ales","created":"2015-10-19T04:27:11.649Z"}]

Currently, It doesn't display any data. I have tried pure JS instead of jquery but it doesn't help.

This is what I got in the Chrome Console : GET localhost:3000/categories?callback=jQuery1113012827125121839345_1445236000644&_=‌​1445236000645 net::ERR_UNKNOWN_URL_SCHEME

Added http:// - does not make a difference

Shashank
  • 329
  • 4
  • 21
  • Make sure your domain `localhost:3000/categories` is working ?? – Altmish-E-Azam Oct 19 '15 at 06:16
  • `JSONP` is meant for that. You can pass your `callback` in the url – Sandeep Nayak Oct 19 '15 at 06:17
  • You have valid JSON, so next step as suggested is that your server is actually working. If you visit http://localhost:3000/categories does it show your data?? If not, you have a server side issue that needs resolving. You can route the JSON `from` another server using a proxy script.. a PHP example would be using http://stackoverflow.com/questions/29997705/how-to-bypass-cross-origin-policy/29998032#29998032 as you will see my answer in there – Angry 84 Oct 19 '15 at 06:20
  • It is working, but unfortunately the data on port 8888 does not display. Is my code alright though ? – Shashank Oct 19 '15 at 06:23
  • Try using the proxy script i supplied, while i have never really bothered using ajax across ports... Some how i get the feeling it will be a cross origin error.. Use the console in your browser, goto the network tab and look at the response for the ajax call.. It will give you a 200,404,500 or similar code. – Angry 84 Oct 19 '15 at 06:25
  • As in the proxy script would be hosted on port 8888, and it does a post/get/request to port 3000.. – Angry 84 Oct 19 '15 at 06:26
  • I checked the console in chrome and got this error : GET localhost:3000/categories?callback=jQuery1113012827125121839345_1445236000644&_=1445236000645 net::ERR_UNKNOWN_URL_SCHEME – Shashank Oct 19 '15 at 06:28
  • @Shashank: Do you need to mention the protocol http:// in the url? – Mohamed Hussain Oct 19 '15 at 07:07
  • @MohamedHussain Hi ! I just tired that ... does not make a difference – Shashank Oct 19 '15 at 07:24
  • @Shashank Please check this link https://learn.jquery.com/ajax/working-with-jsonp/ and also as you are using jsonp, you need to mention the callback in the server localhost:8888 – Mohamed Hussain Oct 19 '15 at 08:33
  • check this link http://stackoverflow.com/questions/11736431/make-cross-domain-ajax-jsonp-request-with-jquery – Mohamed Hussain Oct 19 '15 at 08:40

0 Answers0