views:

11

answers:

1

I am using plupload to perform multipart file uploads. On success, it returns the serverdata that was stored. But i am receiving a string instead of an object containing all my attributes.

This is my controller action:

  def upload
      @user = User.create! :name => params[:photo_name], :swf_uploaded_data => params[:photo]
        respond_to do |format|
          format.js { render :json => @user) }
        end
  end

In my Javascript, i have a function that returns the server response, the gist of which is:

  function(serverResponse) {
    alert(serverResponse);
  });

The output of the serverResponse happens to be a string!! (Ex:

"{'user': {'name':'photo','link':'http://www.google.com/favicon.ico'}}")

Now i just cannot access the link by just doing serverResponse.user.link because it is not a serialized output. How do i resolve this problem?

+2  A: 

Parse the JSON. See http://www.json.org/js.html for a more secure method than eval.

Notably var finalObject = JSON.parse(jsonString);

ItzWarty
Cool. Thanks for that answer. :)
Shripad K
I just found out that there exists one in jquery: $.parseJSON. Better for cross browser JSON parsing. Realized that JSON.parse is not supported by Mobile Safari.
Shripad K