javascript 如何从 flickr API 获取正确的 JSON 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24671709/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 03:12:07  来源:igfitidea点击:

how to get correct JSON object from flickr API

javascriptjqueryjsonangularjsflickr

提问by ankitr

I used flickr photo search method to retrieve public photos. when I run it with jquery, it works fine, I get the json object in correct form.

我使用 flickr 照片搜索方法来检索公共照片。当我使用 jquery 运行它时,它工作正常,我以正确的形式获取了 json 对象。

{
    "photos": {
        "photo": [
            {
              .........
            }
        ]
    },
    "stat": "ok"
}

But when I use it with AngularJs, I got the same object with a prefix jsonFlickrApi

但是当我将它与 AngularJs 一起使用时,我得到了带有前缀 jsonFlickrApi 的相同对象

jsonFlickrApi({
    "photos": {
        "photo": [
            {
               ...... 
            }
        ]
    },
    "stat": "ok"
})

what I used in AngularJs is:

我在 AngularJs 中使用的是:

myApp.service('dataService', function($http) {
    delete $http.defaults.headers.common['X-Requested-With'];
    this.flickrPhotoSearch = function() {
        return $http({
            method: 'GET',
            url: 'https://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=3f807259749363aaa29c2fa93945&tags=india&format=json&callback=?',
            dataType: 'json'
         });
     }
});

Please tell me how can I convert the second JSON to the first one. Is there anything I can do in $httpcall or have to alter JSON object.

请告诉我如何将第二个 JSON 转换为第一个。有什么我可以在$http通话中做或必须更改 JSON 对象的吗?

回答by ankitr

There was problem in the url. This Url works for me.

网址有问题。这个网址对我有用。

https://api.flickr.com/services/rest/?method=flickr.people.getPublicPhotos&api_key=3f807259749363aaa29c712fa93945&user_id=61495424@N00&format=json&nojsoncallback=?

回答by maxwellnewage

You have to add nojsoncallback=1 to the url, like this

你必须在 url 中添加 nojsoncallback=1,就像这样

https://api.flickr.com/services/rest/?method=flickr.photos.getRecent&api_key=XXX&format=json&nojsoncallback=1

And that remove the jsonFlickrApi function on response.

并删除响应中的 jsonFlickrApi 函数。

回答by Alan Scarpa

The above answer which says to add nojsoncallback=1is correct, but I'd like to provide more context and reasoning behind it.

上面说添加的答案nojsoncallback=1是正确的,但我想提供更多的上下文和背后的推理。

If you take a look at the documentation (which is poorly formatted in my opinion): https://www.flickr.com/services/api/response.json.htmland scroll all the way to the bottom you will find, tucked away:

如果你看一下文档(我认为它的格式很差):https: //www.flickr.com/services/api/response.json.html并一直滚动到底部你会发现,隐藏离开:

Callback Function
If you just want the raw JSON, with no function wrapper, add the parameter nojsoncallback with a value of 1 to your request.

To define your own callback function name, add the parameter jsoncallback with your desired name as the value.

nojsoncallback=1    -> {...}
jsoncallback=wooYay -> wooYay({...});

The key part being: If you just want the raw JSON, with no function wrapper, add the parameter nojsoncallback with a value of 1 to your request

关键部分是:如果您只想要原始 JSON,没有函数包装器,请将值为 1 的参数 nojsoncallback 添加到您的请求中

Therefore, add this to your request and it will remove jsonFlickrAPIwrapper that you are seeing in your response: nojsoncallback=1

因此,将此添加到您的请求中,它将删除jsonFlickrAPI您在响应中看到的包装器:nojsoncallback=1

回答by Fabio Milheiro

You are getting a JSONPresponse (notice the P) which is basically a function call with an argument that is the response you are expecting.

你得到一个JSONP响应(注意 P),它基本上是一个函数调用,参数是你期望的响应。

Easy solution for you: Create a function named jsonFlickrApiwith a parameter responseand you can do your handing in there.

为您提供简单的解决方案:创建一个以jsonFlickrApi参数命名的函数response,您可以在那里进行处理。

At this point, I am not sure if you can define your function inside your service but try it. If not, you can define it outside.

在这一点上,我不确定您是否可以在您的服务中定义您的函数,但请尝试一下。如果没有,你可以在外面定义它。

Alternatively, try using $http.get function instead. Were you specifying the return accepted by the client in your jQuery original code?

或者,尝试改用 $http.get 函数。您是否在 jQuery 原始代码中指定了客户端接受的返回?

EDIT

编辑

Try this setting the data type of the request using $http or $http.getwithout forgetting to specify the data property in the settings object.

尝试使用 $http 或 $http.get 设置请求的数据类型,而不要忘记在设置对象中指定数据属性。

$http({
    url: 'http://localhost:8080/example/teste',
    dataType: 'json',
    method: 'POST',
    data: '',
    headers: {
        "Content-Type": "application/json"
    }

}).success(function(response){
    $scope.response = response;
}).error(function(error){
    $scope.error = error;
});