角JSONP调用字preSS JSON REST API [英] Angular JSONP calling WordPress JSON REST API

查看:97
本文介绍了角JSONP调用字preSS JSON REST API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不能完全肯定这是怎么回事,因为我还在试图找出JavaScript调用跨域,但我打电话从Word preSS网站的一些JSON(这是使用Word preSS JSON REST API插件)。因为它是从本地站点来(我正在使用离子/角移动应用),我有一些跨域访问的问题。所以,我期待在使用JSONP。

我已经成功地打了个电话,回来的信息,但我似乎无法做返回的数据什么。

我的电话是像这样:

<$p$p><$c$c>$http.jsonp('http://heavymetalhamilton.ca/wp-json/hmh-api/shows?_jsonp&callback=JSON_CALLBACK')
  .success(功能(数据){
    的console.log(数据);
  });

但是,没有被记录。我可以看到$ http.jsonp调用返回的数据(我可以在开发者工具看到和看起来像这样:

  / ** /([{ID:30,POST_DATE:2014年9月25日18点33分15秒,post_date_gmt:2014-09 -25
...

我看到返回数据的周围有一些包装,但我不知道如何处理这些数据。

任何帮助吗?

编辑:有什么发生在 codePEN


解决方案

所以,我有最大的问题(有一对夫妇),当时我JSONP电话。没有连接到它的成功的承诺。 JSONP触发定义为一个回调函数 - 在这种情况下,呼叫到

http://heavymetalhamilton.ca/wp-json/hmh-api/shows?_jsonp=mytest&callback=JSON_CALLBACK

调用的mytest的一个全局函数()返回一次。因此,为了得到一个全球性的功能,在我的控制器,这样我就可以使用$范围,我的最终结果是:

<$p$p><$c$c>$http.jsonp('http://heavymetalhamilton.ca/wp-json/hmh-api/shows?_jsonp=mytest&callback=JSON_CALLBACK');window.mytest =功能(数据){
  $ scope.shows =数据;
};

我还与我回来(如@Miszy提到的)内容类型的问题。我必须指定的内容类型设置为应用程序/ JavaScript的... previously它返回应用程序/ JSON。

在我的情况,我写了一些自定义功能回到具体的内容,所以我增加了以下内容:

 如果($ _ GET ['_ JSONP']){
   $响应 - &GT;标题(内容类型,应用程序/ JavaScript的;字符集='get_option('blog_charset'),TRUE);
}

希望这可以帮助别人的道路!

I'm not entirely sure what's going on, as I'm still trying to figure cross domain javascript calls, but I'm calling some JSON from a WordPress site (that's using the WordPress JSON REST API plugin). Since it's coming from a local site (I'm working on a mobile app using Ionic/Angular), I've had some cross-domain access issues. So, I'm looking at using JSONP.

I've successfully called and returned info, but I can't seem to do anything with the returned data.

My call is as so:

$http.jsonp('http://heavymetalhamilton.ca/wp-json/hmh-api/shows?_jsonp&callback=JSON_CALLBACK')
  .success(function(data) {
    console.log(data);
  });

However, nothing is being logged. I can see that the $http.jsonp call is returning data (I can see in developer tools and looks like this:

/**/([{"ID":30,"post_date":"2014-09-25 18:33:15","post_date_gmt":"2014-09-25 
...
)

I see that the returning data has some packing around it, but I'm not sure how to process this data.

Any help?

EDIT: Have what's happening going on in a CodePen

解决方案

So, the biggest problem I had (there were a couple), was my jsonp call. There isn't a "success" promise attached to it. Jsonp fires a function defined as a callback - in this case, the call to:

http://heavymetalhamilton.ca/wp-json/hmh-api/shows?_jsonp=mytest&callback=JSON_CALLBACK

calls a global function of mytest() once returned. So, in order to get a global function in my controller, so I can use $scope, my end result is:

$http.jsonp('http://heavymetalhamilton.ca/wp-json/hmh-api/shows?_jsonp=mytest&callback=JSON_CALLBACK');

window.mytest = function(data) {
  $scope.shows = data;
};

I also had an issue with the content type I was returning (as mentioned by @Miszy). I have to specify that the Content-Type is set to application/javascript... previously it was returning application/json.

In my case, I'm writing some custom functionality to return specific content, so I added the following:

if ($_GET['_jsonp']) {
   $response->header( 'Content-Type', 'application/javascript; charset=' . get_option( 'blog_charset' ), true );
}

Hope this helps someone down the road!

这篇关于角JSONP调用字preSS JSON REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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