LoginSignup
1
2

More than 5 years have passed since last update.

AngularJSの注意点

Posted at

参考サイト

POSTでパラメータを送信する際は、設定が必要

    @testApp.config ($httpProvider) ->
        $httpProvider.defaults.transformRequest = (data = {}) ->
            $.param(data);
        $httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8'

     @

そのままPOST送信するとパラメータがJSON文字列?で送信されるらしく、

うまくデータを送れないので通常のForm送信の方式に合わせる必要がある

ただ、 railsangularjs-rails-resource を使っていると大丈夫な模様

$resourceでPOST or PUT する際のURLパラメータの渡し方。

    url = '/api/action/:id'

    model = $resource url, null ,
        get: {method: 'GET', isArray: false}
        post: {method: 'POST', isArray: false, params: {id: "@id"}
        put: {method: 'PUT', isArray: false, params: {id: "@id"}}
        delete: {method: 'DELETE', isArray: false}

    param =
        id: 1
        name: 'myName'

    model.put param, (res, e) ->
            console.log res

GET と DELETE は そのままで paramidaction/:id
に反映されるが、
PUT と POST の場合は、モデルのメソッド作成の際に指定しておく必要がある模様

    model = $resource url, {id: "@id"} ,
        get: {method: 'GET', isArray: false}
        post: {method: 'POST', isArray: false}
        put: {method: 'PUT', isArray: false}
        delete: {method: 'DELETE', isArray: false}

これでも大丈夫そう。

1
2
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
1
2