解析在angular.js JSONP $ http.jsonp()响应 [英] parsing JSONP $http.jsonp() response in angular.js

查看:97
本文介绍了解析在angular.js JSONP $ http.jsonp()响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是成功返回JSON包裹在一个函数角度的 $ http.jsonp()要求:

I am using angular's $http.jsonp() request which is successfully returning json wrapped in a function:

var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=jsonp_callback";

$http.jsonp(url).
    success(function(data, status, headers, config) {
        //what do I do here?
    }).
    error(function(data, status, headers, config) {
        $scope.error = true;
    });

问题是,我不知道如何访问/解析返回的功能包裹-JSON。任何指导多少AP preciated。谢谢!

The problem is, I don't know how to access/parse the returned function-wrapped-JSON. Any guidance is much appreciated. Thanks!

推荐答案

所有你应该做的是更换回调= jsonp_callback 回调= JSON_CALLBACK 像这样:

All you should have to do is change callback=jsonp_callback to callback=JSON_CALLBACK like so:

var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=JSON_CALLBACK";

然后你的 .success 函数应该火象你拥有了它,如果返回成功。

And then your .success function should fire like you have it if the return was successful.

这样做可以让你不必脏了全球性的空间。这是记录在这里的AngularJS文档

Doing it this way keeps you from having to dirty up the global space. This is documented in the AngularJS documentation here.

更新马特球的小提琴使用这个方法: http://jsfiddle.net/subhaze/a4Rc2/114/

Updated Matt Ball's fiddle to use this method: http://jsfiddle.net/subhaze/a4Rc2/114/

完整的示例:

var url = "http://public-api.wordpress.com/rest/v1/sites/wtmpeachtest.wordpress.com/posts?callback=JSON_CALLBACK";

$http.jsonp(url)
    .success(function(data){
        console.log(data.found);
    });

这篇关于解析在angular.js JSONP $ http.jsonp()响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆