【问题标题】:Can't get JSON from external service even using JSONP即使使用 JSONP 也无法从外部服务获取 JSON
【发布时间】:2015-11-24 07:11:04
【问题描述】:

您好,我正在尝试使用来自网站的 json 数据,但我无法使用 jsonp 执行此操作,不确定是我编码错误还是服务问题。

   <script>
    $(function () {
        $("#frmInstrumento").submit(function(event) {
            alert("hello");
            $.ajax({
                url: "https://www.camaradenegocios.com/api/get/",
                // The name of the callback parameter, as specified by the YQL service
                jsonp: "promotions",

                // Tell jQuery we're expecting JSONP
                dataType: "jsonp",

                // Work with the response
                success: function( response ) {
                    alert(response);
                    console.log( response ); // server response
                }
            });
        });
    });
    </script>

要使用的 url 是 https://www.camaradenegocios.com/api/get/promotions 如果我浏览,我可以看到数据,但不能使用 Jquery 使用它。

【问题讨论】:

  • 请将error属性放在success属性之后,并给它一个console.log()或者alert()来显示错误,然后报告。
  • 跨源请求被阻止:同源策略不允许读取位于camaradenegocios.com/api/get 的远程资源。 (原因:CORS 请求失败)。

标签: jquery json jsonp


【解决方案1】:

现在我让它工作了,不知道提供者出了什么问题,无论如何。我也在阅读有关提交的内容,因为它会回发它不会检索数据。

  $(function(){
      loadData = function(){
        $.ajax({
            url: 'https://www.camaradenegocios.com/api/get/promotions',
            method: 'GET',
            dataType: 'json',
            crossDomain: true,
            success: function(response){ 
                $.each(response, function(key, data) {
                    alert(key);
                    $.each(data, function (index, data) {
                        console.log('index', data);
                        alert(data);
                    })
                })      
            }
        });    
      };

      loadData();
  });

现在只有我必须正确解析数据。

谢谢。

【讨论】:

    【解决方案2】:

    尝试使用$.getJSON();响应似乎是 JSON ,而不是 JSONP

    $.getJSON("https://www.camaradenegocios.com/api/get/promotions"
    , function(data) {
      console.log(JSON.stringify(data, null, 2));
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
    </script>

    【讨论】:

    • 回调参数是干什么用的? URL 没有返回 jsonp
    • @charlietfl 你是对的。查看更新后的帖子;从网址中删除了不必要的?callback=
    【解决方案3】:

    我希望你已经包含了 jQuery 库。如果没有,请在您的 HTML 页面标题中添加类似的内容:

    &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"&gt;&lt;/script&gt;

    我认为如果您尝试获取 JSON 数据而不是 JSONP,它应该可以工作。用下面提到的代码替换您的代码将按预期工作,获取我们在浏览 URL 时看到的响应数组。

    <script>
    $(function () {
        $("#frmInstrumento").submit(function(event) {
            alert("hello");
            $.ajax({
                url: "https://www.camaradenegocios.com/api/get/promotions",
                // The name of the callback parameter, as specified by the YQL service
    
                // Tell jQuery we're expecting JSON
                dataType: "json",
    
                // Work with the response
                success: function( response ) {
                    alert(response);
                    console.log( response ); // server response
                }
            });
        });
    });
    </script>
    

    现在response 将获得原始响应,您必须对其进行字符串化以使其具有可读格式。您也可以将此响应作为一个对象进行操作,并从中提取属性和成员。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-21
    • 1970-01-01
    • 1970-01-01
    • 2014-03-10
    • 2017-04-18
    • 2018-05-27
    • 1970-01-01
    • 2014-04-19
    相关资源
    最近更新 更多