Wednesday, 15 May 2013

javascript - angular http post adds extra params to the request -



javascript - angular http post adds extra params to the request -

i have next in directive:

scope.progresscourse = -> req_data = course_id: scope.course.id success: true $http.post( "<%= rails.application.routes.url_helpers.progress_course_path %>", req_data ).then (succesful_resp) -> scope.course = succesful_resp.data.data scope.init_from_course_object() , (error_resp) -> show_error(error_resp)

and, server side, i'd expect receive course_id , success params. however, receive extra:

parameters: {"course_id"=>1, "success"=>true, "training"=>{"course_id"=>1, "success"=>true}}

the request addressed controller called trainingcontroller. i'm running rails 3.2.13, angular 1.2.10. explain why hash appears {"training"=>{"course_id"=>1, "success"=>true}}

update: if add together this:

$httpprovider.defaults.transformrequest = (data) -> if data? homecoming $.param(data) homecoming info

and alter post to:

$http( method: "post" url: "<%= rails.application.routes.url_helpers.progress_course_path %>", data: req_data, headers: { 'content-type': 'application/x-www-form-urlencoded; charset=utf-8'} ).then (succesful_resp) ->

then 2 params need. can explain why behaviour?

rails has lot of inbuilt functionality create dealing external apis easier. in case has params_wrapper manipulate parameters passed in in request, nesting them in hash assumes want.

for example, if submitting {name: 'foo', age: 12} userscontroller, assume should wrapped in nested user key , transparently.

documentation wrapper here:

http://api.rubyonrails.org/v3.2.13/classes/actioncontroller/paramswrapper.html

and code here:

https://github.com/rails/rails/blob/v3.2.13/actionpack/lib/action_controller/metal/params_wrapper.rb

if don't want functionality, can disable commenting out wrap_parameters format: [:json] line in /config/initializers/wrap_parameters.rb.

javascript jquery ruby-on-rails angularjs

No comments:

Post a Comment