即使使用JSONP,也无法从外部服务获取JSON

时间:2015-08-29 22:05:50

标签: jquery json jsonp

您好我正在尝试使用来自某个网​​站的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>

要使用的网址是https://www.camaradenegocios.com/api/get/promotions我可以在浏览时看到数据,但不能使用Jquery来使用它。

3 个答案:

答案 0 :(得分:0)

尝试使用$.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>

答案 1 :(得分:0)

我希望你已经包含了jQuery库。如果没有,请在HTML页面标题中输入以下内容:

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

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

response

现在<?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl" > <xsl:output method="xml" indent="yes"/> <xsl:template match="@* | node()"> <xsl:copy> <xsl:apply-templates select="@* | node()"/> </xsl:copy> </xsl:template> <xsl:template match="/"> <book> <xsl:for-each select="book/page"> <div class="content"> <div class="nav"> <a class="prev" id="prev">Previous Page</a> <span class="sep">|</span> <div class="title" id="divTitle"> <xsl:value-of select="title" /> </div> <span class="sep2">|</span> <a class="next" id="next">Next Page</a> </div> <div class="main"> <xsl:value-of select="content"/> </div> <div id="thisPage" class="page"> <xsl:value-of select="pgno" /> </div> </div> </xsl:for-each> </book> </xsl:template> </xsl:stylesheet> 将具有原始响应,您必须将其字符串化以使其具有可读格式。您也可以将此响应作为对象进行播放,并从中提取属性和成员。

答案 2 :(得分:0)

现在我开始工作了,不知道提供商出了什么问题。此外,我正在阅读有关提交的内容,因为它会进行回发,但它无法检索数据。

  $(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();
  });

现在只需要正确解析数据。

感谢。

相关问题