Unable to Parse JSON Response from Rails Controller in Javascript -


i'm working in rails 4 app , using best_in_place ajax update user's address field.

the ajax call works , in update action in controller have respond_to block i'd spit out user id, new updated address , boolean if address valid or not.

respond_to |format|   format.json { render :json => { :id => @user.id, :address => @user.address, :address_valid => @user.address_valid } } end 

in javascript, i'm trying parse json. have tried both ['id'] , dot notation. here's js function listen json after successful update:

$('.best_in_place').bind("ajax:success", function (response, data) {     console.log(response)     console.log(data)     console.log(typeof data)   }); }); 

the console logs response json controller predictably:

jquery.event {type: "ajax:success", timestamp: 1401820902436, jquery1110046177322673611343: true, istrigger: 3, namespace: ""…} {"id":704,"address":"austin, tx 78751, usa","address_valid":false}  string 

i'm getting 'undefined' data.id. i've tried calling ## heading ##

so had typed question figured i'd post because found answer , figured might someone: needed use

json.parse()  

in js , set json object variable. there, able access json object.

var user = json.parse(data)  

from there, able call user.id, user.address, etc. in js.


Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -