【问题标题】:Return JSON from Rest server in JavaScript [duplicate]在 JavaScript 中从 Rest 服务器返回 JSON [重复]
【发布时间】:2015-12-14 12:49:26
【问题描述】:

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

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

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

};

【问题讨论】:

    标签: javascript jquery json rest


    【解决方案1】:

    首先,它是$.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.
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2012-08-06
      • 1970-01-01
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      • 2012-10-21
      相关资源
      最近更新 更多