22
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【AngularJS】JSONPを受け取る

22
Last updated at Posted at 2014-04-07

jQueryだと$.ajax({url: url, dataType: 'jsonp'})でJSONPを読み込める.
じゃあAngularJSでは?

やりかた

$http.jsonpを使おう!

コールバック関数名がJSON_CALLBACKになるようにパラメータを設定してあげればOK.
(これはAPI提供側が対応してないといけない?)

test.js
$http.jsonp('http://example.com/api/test?callback=JSON_CALLBACK').success(function(response){
  // callback
});

パラメータをオブジェクトで渡したいとき

test.js
$http.jsonp('http://example.com/api/test', {params: {callback: 'JSON_CALLBACK'}).success(function(response){
  // callback
});

のように,$http.jsonp(url, config)の第2引数の1要素として渡してあげないといけない.
$.ajaxとかとはちょっとちがう.

22
22
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
22
22

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?