M
M
Maxim Ivanov2016-07-11 11:42:10
JavaScript
Maxim Ivanov, 2016-07-11 11:42:10

How to get json data on localhost?

var HttpServer = function(){
        this.localhost = "http://95.165.159.193/blackbox/service.svc/";
        this.proccessget = function(){

            $.getJSON( this.localhost + "processes/get", function( data ) {
              alert(data)
            });

        }
    };

    var service = new HttpServer();


I launch the site on localhost:8080
A third-party service is launched at 95.165.159.193 and returns json

Tried to connect by executing the service.proccessget() function
However, it gives an error, what should I do?

XMLHttpRequest cannot load http://95.165.159.193/blackbox/service.svc/processes/get. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8080' is therefore not allowed access.


If you use jsonp, then it gives a syntax error at the response body
var HttpServer = function(){
        this.localhost = "http://95.165.159.193/blackbox/service.svc/";
        this.proccessget = function(){

            $.ajax({
                url: this.localhost + "processes/get",
                crossDomain: true,
                dataType: 'jsonp',
                success: function( data ) {
                    alert(data)
                }
            });

        }
    };

    var service = new HttpServer();


get?callback=jQuery2110606…_1468226898915&_=1468226898916:1 Uncaught SyntaxError: Unexpected token :

ec3a19e5bfbb4755a98560f6ea4be21c.png

95.165.159.193 is not my server

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mikhail Osher, 2016-07-11
@splincodewd

Read about the basics of CORS.
If this is your service (which is on IP 95.165.159.193) - add the appropriate headers.
Otherwise, either jsonp or proxy the request through your backend.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question