簡體   English   中英

如何從 javascript 中的 url 中獲取 json 數據?

[英]How to get json data from url in javascript?

我有 url 和 json 數據。 例如: {"type": "feature", "name": name"...}

URL 是 GeoServer URL。

我想獲取 JSON 數據。

我應該怎么辦?

1) $.ajax(url,
       { dataType: "jsonp" }
   ).done(function ( data ) {
       console.log('done will never be called, unfortunately...');
   });

2) $.ajax({
   jsonp:false,
   jsonpCallback: 'getJson',
   type: 'GET',
   url: url,
   async:false,
   dataType: 'jsonp',
   success: function(data){
       alert('success');
   }
  });

3) var view = map.getView();
          var viewResolution = view.getResolution();
          var source = untiled.get('visible') ? untiled.getSource() : tiled.getSource();
          var url = source.getGetFeatureInfoUrl(
            coordinate, viewResolution, view.getProjection(),
      {'INFO_FORMAT': 'application/json', 'FEATURE_COUNT': 50});

       if (url) {

              $.ajax({
               jsonp:false,
               jsonpCallback: 'getJson',
               type: 'GET',
               url: url,
               async:false,
               dataType: 'jsonp',
               success: function(data){
                   alert('success');
               }
           });
       }

但這不起作用。 我沒有得到 json 數據。

嘗試下面的 Ajax cal 選項,這些選項已經解決了成功和錯誤。如果有任何錯誤,很容易找到。

$.ajax({
      type: "GET",
      url: url,
      dataType: "json",
      contentType: "application/json",
      crossDomain: true,
      data:'',
      success: function (response) {
        var res = JSON.parse(response);
        // Console response data
        console.log(res);
        // Do your Operations here...
      },

      error: function (jqXHR, error, errorThrown) {
        console.log(error);
      }
    });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM