简体   繁体   English

通过jQuery AJAX从远程URL检索JSON数据

[英]Retrieve JSON data from remote URL via jQuery AJAX

I am using following code to get the data from URL. 我正在使用以下代码从URL获取数据。

$.ajax({
    url: 'http://183.77.251.173:90/api/function/getprice.aspx?code=1301&length=3M',
    success: function (data) {
        alert(data.results[0].address_components[0].long_name);
    },
    error: function (jqXHR, exception) {
        alert(jqXHR.status);
    }

However it throws an error with status code of 0 . 但是,它将引发错误,状态代码为0 I don't know what the reason is for this? 我不知道这是什么原因? I tried to set crossDomian:true also but it still throws same error. 我也尝试设置crossDomian:true但是它仍然抛出相同的错误。

I also modified the URL to http://www.google.com which also returns the error status code of 0 . 我还将URL修改为http://www.google.com ,该URL还返回了错误状态代码0 Why? 为什么? What is the reason? 是什么原因? What is the correct way to get the data from a remote URL? 从远程URL获取数据的正确方法是什么?

You cannot make a cross domain request unless you're using JSONP or CORS. 除非使用JSONP或CORS,否则您无法发出跨域请求。 The availability of those will depend on the API of the domain you are requesting information from. 这些资源的可用性将取决于您从中请求信息的域的API。

This is a security feature of modern browsers known as the Same Origin Policy . 这是现代浏览器的安全功能,称为“ 相同来源策略”

Look up JSON with padding (or JSONP), since you use jQuery, you should take a look here: http://api.jquery.com/jQuery.getJSON/ 使用填充(或JSONP)查找JSON,由于您使用jQuery,因此应在此处查看: http : //api.jquery.com/jQuery.getJSON/

Stolen example from that site: 该网站的示例被盗:

<script>
(function() {
  var flickerAPI = "http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?";
  $.getJSON( flickerAPI, {
    tags: "mount rainier",
    tagmode: "any",
    format: "json"
  })
  .done(function( data ) {
    $.each( data.items, function( i, item ) {
      $( "<img/>" ).attr( "src", item.media.m ).appendTo( "#images" );
      if ( i === 3 ) {
        return false;
      }
    });
  });
})();
</script>

EDIT: 编辑:

An homemade example, using jquery.jsonp-2.4.0 for better error response ( https://github.com/jaubourg/jquery-jsonp/downloads ). 一个自制的示例,使用jquery.jsonp-2.4.0以获得更好的错误响应( https://github.com/jaubourg/jquery-jsonp/downloads )。 But you can use plain jQuery as well. 但是您也可以使用普通的jQuery。

On the client side, you need something like this: 在客户端,您需要这样的东西:

$.jsonp({
    "url": target_url+"ping.php?callback=?",
    "success": function(data) {
        // print out data
    },
    "error": function(d,msg) {
        // error
    }
});

The ping.php file on target server: 目标服务器上的ping.php文件:

<?php
    echo $_GET['callback'] . '(' . "{'response' : 'success'}" . ')';
?>

As others have said you're getting error 0 is because the site is unreachable. 正如其他人所说,您收到错误0的原因是该网站无法访问。 Cross site issue is a reason. 跨站点问题是一个原因。 Of course so is an incorrect URL. 当然,不正确的URL也是如此。 If the URL you're trying to reach does work and is on a different domain than your site then yes you have a cross domain issue. 如果您尝试访问的URL确实有效并且位于与您的网站不同的域上,那么您有跨域问题。

JSONP is going to be your only way to get it to work but there's drawbacks. JSONP将是您使其正常工作的唯一方法,但存在弊端。 Have a look at this post on SO for detailed explanation: 看一下关于SO的这篇文章以获取详细说明:

What is JSONP all about? JSONP的全部含义是什么?

There's also a link in the article to http://www.json.org/JSONRequest.html . 文章中还有一个指向http://www.json.org/JSONRequest.html的链接。 I haven't tried this so not sure if it works. 我还没有尝试过,所以不确定是否可行。

This should help you on your way. 这应该对您有帮助。

For cross-domain use $.getJSON() . 对于跨域,请使用$.getJSON()

$.getJSON('http://183.77.251.173:90/api/function/getprice.aspx?code=1301&length=3M', function(data){
alert(data.results[0].address_components[0].long_name);
});

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

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