简体   繁体   English

无法从网址解析json

[英]Unable to parse the json from url

<html>
    <body>
        <script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
        <script>
            $(document).ready(function () {
                $.ajax({
                    type: 'GET',
                    async: false,
                    contentType: "application/json",
                    url: "http://www.XXX.XXX.in/api/categories",//url:"dir/categories",
                    dataType: "jsonp", //dataType: "jsonp",
                    success: function (data) {
                        $.each(data, function(i,data) {
                            var det="<div>id :"+data.categoryId+" Name "+ data.categoryName+"</div></br>";
                            $(det).appendTo("#display");

                            //alert(det);
                        });
                        alert(data);
                    },
                    error: function (data) {
                        alert("this is error "+data);
                    }
                });
            });

        </script>

        <div id="display"></div>
    </body>
</html>

In the above code I am trying to access the categories json and print the details. 在上面的代码中,我试图访问类别json并打印详细信息。
I am doing it in two ways: 我这样做有两种方式:

I have kept the categories file in dir/ folder and accessing it which shows me result properly. 我将类别文件保存在dir /文件夹中并对其进行访问,这可以正确显示结果。
When I try to access it online it gives me an error: When I give dataType:"json" instead of jsonp I gives following error: 当我尝试在线访问它时,出现一个错误:当我给出dataType:"json"而不是jsonp时,出现以下错误:

OPTIONS http:// XXX.XXX.in/api/categories 404 (Not Found)
XMLHttpRequest cannot load http:// XXX.XXX.in/api/categories. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http:// localhost:8080' is therefore not allowed access.

I dont know whether the server has cross platform ref. 我不知道服务器是否具有跨平台参考。 added. 添加。

You can't access data of another domain from your domain using JAVASCRIPT . 您不能使用JAVASCRIPT从您的域访问另一个域的数据。 It is a security rule known as the " Same origin Policy " 这是一个安全规则,称为“ 相同来源策略

So, to get data of another domain, you could write server side script (maybe in PHP or some other language you're familiar with.), then you can make ajax request from your script to the server. 因此,要获取其他域的数据,您可以编写服务器端脚本(也许使用PHP或您熟悉的其他语言。),然后可以从脚本向服务器发出Ajax请求。

The same origin policy is enforced by the browser to protect websites from other websites making xhr requests and displaying their content as if it was their own. 浏览器强制执行相同的原始策略,以保护网站免受其他提出xhr请求并显示其内容的网站的影响,就好像它是自己的一样。

So site A.com cannot connect to B.com with XHR or: 因此,站点A.com无法使用XHR或以下方式连接到B.com:
http://A.com cannot connect to http://sub.A.com http://A.com无法连接到http://sub.A.com
localhost:80 cannot connect to localhhost:8080 本地主机:80无法连接到本地主机:8080

Edit 编辑

As you requested, here is the solution using PHP script. 根据您的要求,这是使用PHP脚本的解决方案。

get_json_from_url.php get_json_from_url.php

<?php
header("Content-type: text/json");
$response = fopen('http://www.eazydial.logicengine.in/api/categories', "rb");  
echo stream_get_contents($response);
?>  

HTML page HTML页面

<html>
    <body>
        <script src='http://code.jquery.com/jquery-1.10.2.min.js'></script>
        <script>
            $(document).ready(function () {
                $.ajax({
                    type: 'GET',
                    url: "get_json_from_url.php",
                    dataType: "json",
                    success: function (data) {
                        $.each(data, function(i,data) {
                            var det="<div>id :"+data.categoryId+" Name "+ data.categoryName+"</div></br>";
                            $(det).appendTo("#display");

                        });
                        console.log(data); //alert can't print [object] so always use console.log() to inspect object and it values. (see output in browser console)
                    },
                    error: function (data) {
                        console.log("Err:");
                        console.log(data);
                    }
                });
            });

        </script>

        <div id="display"></div>
    </body>
</html>

The solution provided here in PHP script will work only for GET request method. PHP脚本中此处提供的解决方案仅适用于GET请求方法。 If you want to use POST request for any API then look for cURL library to get data from api. 如果要对任何API使用POST请求,请查找cURL库以从api获取数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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