用JavaScript从Rest服务器返回JSON

Return JSON from Rest server in JavaScript

本文关键字:返回 JSON 服务器 Rest JavaScript      更新时间:2023-09-26

我正试图从REST端点服务器返回JSON。我尝试过使用dom.io.script.get(),但它用于访问JSONP。CCD_ 2不允许在其服务器上进行这些请求。我的解决方法是使用jQuery $.getJSON来检索JSON对象,并将其返回以便在JavaScript中使用。这可能吗。这是我用于此功能的psuedo代码

url=http://sampleserver6.arcgisonline.com/arcgis/rest/services/Military/FeatureServer/6?f=pjson

    function GetJson(url){
        $(document).ready(function(){
            $getJson(url, function(json){
            return json;
        });
        });
};

首先,它是$.getJSON()而不是$getJson()。不能从异步调用return。因此,要对使用$.getJSON函数返回的json进行处理,需要将其放入匿名函数中:

$.getJSON(url, function (json) {
  // The scope of json is this. You cannot return this anywhere or set anything.
  // Add your functions or instructions that use `json` here.
});