【问题标题】:Unable to parse the json from url无法从 url 解析 json
【发布时间】:2014-12-18 04:27:41
【问题描述】:
<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>

在上面的代码中,我尝试访问类别 json 并打印详细信息。
我有两种方式:

我已将类别文件保存在 dir/ 文件夹中并访问它以正确显示结果。
当我尝试在线访问它时,它给了我一个错误: 当我给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.

我不知道服务器是否有跨平台参考。已添加。

【问题讨论】:

标签: javascript ajax json


【解决方案1】:

您无法使用JAVASCRIPT 从您的域访问另一个域的数据。这是一个安全规则,称为“Same origin Policy

因此,要获取另一个域的数据,您可以编写服务器端脚本(可能是 PHP 或您熟悉的其他语言。),然后您可以从脚本向服务器发出 ajax 请求。

浏览器强制执行同源策略以保护网站免受其他网站发出 xhr 请求并像显示自己的内容一样显示其内容。

所以站点 A.com 无法使用 XHR 连接到 B.com 或:
http://A.com 无法连接到 http://sub.A.com
localhost:80 无法连接到 localhost:8080

编辑

根据您的要求,这里是使用 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 页面

<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>

此处在 PHP 脚本中提供的解决方案仅适用于 GET 请求方法。如果您想对任何 API 使用 POST 请求,请查找 cURL library 以从 api 获取数据。

【讨论】:

  • 感谢您的快速代表,如果我允许跨域调用表单,服务器将工作或任何其他解决方案..
  • 如果你在http://www.eazydial.logicengine.in/允许跨源,它应该可以工作
  • @user3705817,也请参阅这些线程以获取更多信息linklink2
  • 我对 jsonp 没有深入的了解,我曾经在我的本地 php 脚本上做 ajax,我曾经从 php 脚本调用 api 和 echo json。运气好
  • 如果我想从 php 中的“eazydial.logicengine.in/api/categories”访问类别 json 然后在 jquery 中解析,你能帮我了解如何从我的代码中获取 php 脚本中的 json 吗?
猜你喜欢
  • 1970-01-01
  • 2019-08-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多