简体   繁体   中英

Cross domain JSON Javascript

Hi I am trying to use ONLY JavaScript and HTML to GET the JSON object from an URL. I am using the following code:

<html>
<head>
    <title>Json</title>
    <script src="jquery-3.2.1.min.js"></script>
</head>
    <body>
        <button onclick="myFunction()">Start</button>

        <script>
        function myFunction() {
            jQuery.ajax( {
                url: 'https://iotmmss0018275632trial.hanatrial.ondemand.com/com.sap.iotservices.mms/v1/api/http/app.svc/T_IOT_59824470F0BD12350F22?$format=json',
                type: 'GET',
                crossDomain: true,
                dataType: 'json',
                success: function( response ) {
                    console.log(response);
                }, 
                error : function(error) {
                    console.log(error);
                },  
            });
        }
        </script>

    </body>

As expected I am getting

Failed to load "URL": No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin ' http://localhost ' is therefore not allowed access. The response had HTTP status code 401.

I cant configure the Server and I dont know if it supports CORS (Im new). I tried a JSONP request and got as response status code 200 and the JSON, but with the error:

Uncaught SyntaxError: Unexpected token :

So what can I do?

You should try to go through this answer here . It has covered this topic in details.

PS: Should've commented this but couldn't as I don't have the required reputation points.

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