LoginSignup
22
22

More than 5 years have passed since last update.

【AngularJS】JSONPを受け取る

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